Some operations are difficult or even impossible to obtain with an expression, or the operation could become too complex to achieve. The alternative is to create a function that would take care of performing the operation and supplying the result to the table. In order to involve a function with your data entry, you must have one. You can create your own function. Here is an example: -- ============================================= -- Author: FunctionX -- Create date: Saturday 22 December 2007 -- Description: Used to calculate the greatest common divisor -- ============================================= CREATE FUNCTION GCD ( @a int, @b int ) RETURNS int AS BEGIN DECLARE @Remainder int; WHILE @b <> 0 BEGIN SET @Remainder = @a % @b; SET @a = @b; SET @b = @Remainder; END RETURN @a END When calling the function, follow the normal rules. Here are examples: INSERT INTO Calculations VALUES(345, 135, dbo.GCD(345, 135)); GO INSERT INTO Calculations VALUES(40, 6, dbo.GCD(40, 6)); GO INSERT INTO Calculations VALUES(16, 28, dbo.GCD(16, 28)); GO You can use one of the built-in functions of Transact-SQL. For example, imagine you have a database named AutoRepairShop and imagine it has a table used to create repair orders for customers: CREATE TABLE RepairOrders ( RepairID int Identity(1,1) NOT NULL, CustomerName nvarchar(50), CustomerPhone nvarchar(20), RepairDate DateTime ); GO When performing data entry for this table, you can let the user enter the customer name and phone number. On the other hand, you can assist the user by programmatically entering the current date. To do this, you would call the GETDATE() function. Here are examples: INSERT INTO RepairOrders(CustomerName, CustomerPhone, RepairDate) VALUES('Annette Berceau', '301-988-4615', GETDATE()); GO INSERT INTO RepairOrders(CustomerPhone, CustomerName, RepairDate) VALUES('(240) 601-3795', 'Paulino Santiago', GETDATE()); GO INSERT INTO RepairOrders(CustomerName, RepairDate, CustomerPhone) VALUES('Alicia Katts', GETDATE(), '(301) 527-3095'); GO INSERT INTO RepairOrders(RepairDate, CustomerPhone, CustomerName) VALUES(GETDATE(), '703-927-4002', 'Bertrand Nguyen'); GO You can also involve the function in an operation, then use the result as the value to assign to a field. You can also call a function that takes one or more arguments; make sure you respect the rules of passing an argument to a function when calling it. If none of the Transact-SQL built-in functions satisfies your requirements, you can create your own.
When performing data entry, in some columns, even after indicating the types of values you expect the user to provide for a certain column, you may want to restrict a range of values that are allowed. A check constraint is a Boolean operation performed by the SQL interpreter. To create a check constraint, when creating a table, right-click anywhere in (even outside) the table and click Check Constraints...
This would open the Check Constraints dialog box. To create a check constraint in SQL, first create the column on which the constraint will apply. Before the closing parenthesis of the table definition, use the following formula: CONSTRAINT name CHECK (expression) The CONSTRAINT and the CHECK keywords are required. As an object, make sure you provide a name for it. Inside the parentheses that follow the CHECK operator, enter the expression that will be applied. Here is an example that will make sure that the hourly salary specified for an employee is greater than 12.50: Imports System.Data.SqlClient Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, _ ByVal e As System.EventArgs) Handles Me.Load Using Connect As SqlConnection = _ New SqlConnection("Data Source=(local);" & _ "Database='Exercise';" & _ "Integrated Security=yes;") Dim Command As SqlCommand = _ New SqlCommand("DROP TABLE Employees;" & _ "CREATE TABLE Employees " & _ "( " & _ "[Employee Number] nchar(7), " & _ "[Full Name] nvarchar(80), " & _ "[Hourly Salary] smallmoney, " & _ "CONSTRAINT CK_HourlySalary CHECK ([Hourly Salary] > 12.50) " & _ ");", _ Connect) Connect.Open() Command.ExecuteNonQuery() MsgBox("A new table named Employees has been created") End Using End Sub End Class |
|
|||||
|