SQL Server/T-SQL/Transact SQL/Table Variable

Материал из SQL эксперт
Перейти к: навигация, поиск

Creating Inline User-Defined Functions

   <source lang="sql">

5> 6> CREATE TABLE employee( 7> id INTEGER NOT NULL PRIMARY KEY, 8> first_name VARCHAR(10), 9> last_name VARCHAR(10), 10> salary DECIMAL(10,2), 11> start_Date DATETIME, 12> region VARCHAR(10), 13> city VARCHAR(20), 14> managerid INTEGER 15> ); 16> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

1> 2> 3> CREATE FUNCTION dbo.udf_employee(@EmployeeID int) 4> RETURNS TABLE 5> AS 6> RETURN (SELECT ID, first_name FROM Employee) 7> GO 1> 2> SELECT ID, first_name FROM dbo.udf_employee(2) 3> 4> 5> drop function dbo.udf_employee 6> GO ID first_name


----------
         1 Jason
         2 Alison
         3 James
         4 Celia
         5 Robert
         6 Linda
         7 David
         8 James
         9 Joan

1> 2> drop table employee; 3> GO

</source>
   
  


Define table variable

   <source lang="sql">

1> create table employee( 2> ID int, 3> name nvarchar (10), 4> salary int, 5> start_date datetime, 6> city nvarchar (10), 7> region char (1)) 8> GO 1> 2> insert into employee (ID, name, salary, start_date, city, region) 3> values (1, "Jason", 40420, "02/01/94", "New York", "W") 4> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (2, "Robert",14420, "01/02/95", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (3, "Celia", 24020, "12/03/96", "Toronto", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (4, "Linda", 40620, "11/04/97", "New York", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (5, "David", 80026, "10/05/98", "Vancouver","W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (6, "James", 70060, "09/06/99", "Toronto", "N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (7, "Alison",90620, "08/07/00", "New York", "W") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (8, "Chris", 26020, "07/08/01", "Vancouver","N") 3> GO (1 rows affected) 1> insert into employee (ID, name, salary, start_date, city, region) 2> values (9, "Mary", 60020, "06/09/02", "Toronto", "W") 3> GO (1 rows affected) 1> 2> select * from employee 3> GO ID name salary start_date city region


---------- ----------- ----------------------- ---------- ------
         1 Jason            40420 1994-02-01 00:00:00.000 New York   W
         2 Robert           14420 1995-01-02 00:00:00.000 Vancouver  N
         3 Celia            24020 1996-12-03 00:00:00.000 Toronto    W
         4 Linda            40620 1997-11-04 00:00:00.000 New York   N
         5 David            80026 1998-10-05 00:00:00.000 Vancouver  W
         6 James            70060 1999-09-06 00:00:00.000 Toronto    N
         7 Alison           90620 2000-08-07 00:00:00.000 New York   W
         8 Chris            26020 2001-07-08 00:00:00.000 Vancouver  N
         9 Mary             60020 2002-06-09 00:00:00.000 Toronto    W

(9 rows affected) 1> 2> 3> DECLARE @MyTable Table ( 4> OrderID int, 5> Name char (50) 6> ) 7> 8> INSERT INTO @MyTable 9> SELECT ID, Name 10> FROM Employee 11> WHERE ID BETWEEN 1 AND 10 12> 13> SELECT * FROM @MyTable 14> 15> 16> drop table employee 17> GO (9 rows affected) OrderID Name


--------------------------------------------------
         1 Jason
         2 Robert
         3 Celia
         4 Linda
         5 David
         6 James
         7 Alison
         8 Chris
         9 Mary

(9 rows affected) 1>

      </source>
   
  


Function to pass an array

   <source lang="sql">


4> CREATE FUNCTION dbo.udf_ParseArray( @StringArray varchar(max),@Delimiter char(1) ,@MinRowSelect int,@MaxRowSelect int) 5> RETURNS @StringArrayTable TABLE (RowNum int IDENTITY(1,1), Val varchar(50)) 6> AS 7> BEGIN 8> DECLARE @Delimiter_position int 9> IF RIGHT(@StringArray,1) != @Delimiter 10> SET @StringArray = @StringArray + @Delimiter 11> WHILE CHARINDEX(@Delimiter, @StringArray) <> 0 12> BEGIN 13> SELECT @Delimiter_position = CHARINDEX(@Delimiter, @StringArray) 14> INSERT @StringArrayTable VALUES (left(@StringArray, @Delimiter_position - 1)) 15> SELECT @StringArray = stuff(@StringArray, 1, @Delimiter_position, "") 16> END 17> DELETE @StringArrayTable 18> WHERE RowNum < @MinRowSelect OR RowNum > @MaxRowSelect 19> RETURN 20> END 21> GO 1> 2> SELECT RowNum, Val 3> FROM udf_ParseArray("A,B,C,D,E,F,G", ",", 3, 5) 4> GO RowNum Val


--------------------------------------------------
         3 C
         4 D
         5 E

1> 2> drop function dbo.udf_ParseArray 3> GO 1>

</source>
   
  


Legal custorderdetails Inline Table-Valued Function

   <source lang="sql">

5> 6> CREATE TABLE employee( 7> id INTEGER NOT NULL PRIMARY KEY, 8> first_name VARCHAR(10), 9> last_name VARCHAR(10), 10> salary DECIMAL(10,2), 11> start_Date DATETIME, 12> region VARCHAR(10), 13> city VARCHAR(20), 14> managerid INTEGER 15> ); 16> GO 1> INSERT INTO employee VALUES (1, "Jason" , "Martin", 5890,"2005-03-22","North","Vancouver",3); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (2, "Alison", "Mathews",4789,"2003-07-21","South","Utown",4); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (3, "James" , "Smith", 6678,"2001-12-01","North","Paris",5); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (4, "Celia" , "Rice", 5567,"2006-03-03","South","London",6); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (5, "Robert", "Black", 4467,"2004-07-02","East","Newton",7); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (6, "Linda" , "Green" , 6456,"2002-05-19","East","Calgary",8); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (7, "David" , "Larry", 5345,"2008-03-18","West","New York",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (8, "James" , "Cat", 4234,"2007-07-17","West","Regina",9); 2> GO (1 rows affected) 1> INSERT INTO employee VALUES (9, "Joan" , "Act", 6123,"2001-04-16","North","Toronto",10); 2> GO (1 rows affected) 1> 2> select * from employee; 3> GO id first_name last_name salary start_Date region city managerid


---------- ---------- ------------ ----------------------- ---------- -------------------- -----------
         1 Jason      Martin          5890.00 2005-03-22 00:00:00.000 North      Vancouver                      3
         2 Alison     Mathews         4789.00 2003-07-21 00:00:00.000 South      Utown                          4
         3 James      Smith           6678.00 2001-12-01 00:00:00.000 North      Paris                          5
         4 Celia      Rice            5567.00 2006-03-03 00:00:00.000 South      London                         6
         5 Robert     Black           4467.00 2004-07-02 00:00:00.000 East       Newton                         7
         6 Linda      Green           6456.00 2002-05-19 00:00:00.000 East       Calgary                        8
         7 David      Larry           5345.00 2008-03-18 00:00:00.000 West       New York                       9
         8 James      Cat             4234.00 2007-07-17 00:00:00.000 West       Regina                         9
         9 Joan       Act             6123.00 2001-04-16 00:00:00.000 North      Toronto                       10

(9 rows affected) 1> 2> 3> CREATE FUNCTION dbo.custorderdetails( 4> @custid char(5) 5> ) 6> RETURNS TABLE 7> AS 8> RETURN SELECT 9> OD.ID, 10> OD.Salary 11> FROM 12> Employee AS OD 13> WHERE 14> OD.ID = @custid 15> GO 1> 2> select * from dbo.custorderdetails("1") 3> 4> drop function dbo.custorderdetails 5> GO ID Salary


------------
         1      5890.00

(1 rows affected) 1> 2> 3> 4> drop table employee; 5> GO 1>

</source>
   
  


Use return statement to return a table from a function

   <source lang="sql">

2> 3> CREATE TABLE Employees ( 4> EmployeeID int NOT NULL , 5> LastName nvarchar (20) NOT NULL , 6> FirstName nvarchar (10) NOT NULL , 7> Title nvarchar (30) NULL , 8> TitleOfCourtesy nvarchar (25) NULL , 9> BirthDate datetime NULL , 10> HireDate datetime NULL , 11> Address nvarchar (60) NULL , 12> City nvarchar (15) NULL , 13> Region nvarchar (15) NULL , 14> PostalCode nvarchar (10) NULL , 15> Country nvarchar (15) NULL , 16> HomePhone nvarchar (24) NULL , 17> Extension nvarchar (4) NULL , 18> Photo image NULL , 19> Notes ntext NULL , 20> ReportsTo int NULL , 21> PhotoPath nvarchar (255) NULL 22> 23> ) 24> GO 1> 2> 3> 4> CREATE TABLE RegionPlace 5> (RegionID nvarchar (20) NOT NULL , 6> TerritoryDescription nchar (50) NOT NULL , 7> RegionID int NOT NULL 8> ) 9> GO 1> 2> Insert Into RegionPlace Values ("01581","Westboro",1) 3> Insert Into RegionPlace Values ("01730","Bedford",1) 4> Insert Into RegionPlace Values ("01833","Georgetow",1) 5> Insert Into RegionPlace Values ("95060","Santa Cruz",2) 6> Insert Into RegionPlace Values ("98004","Bellevue",2) 7> Insert Into RegionPlace Values ("98052","Redmond",2) 8> Insert Into RegionPlace Values ("98104","Seattle",2) 9> Go (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> CREATE TABLE EmployeeRegion 2> (EmployeeID int NOT NULL, 3> RegionID nvarchar (20) NOT NULL 4> ) 5> GO 1> 2> Insert Into EmployeeRegion Values (1,"06897") 3> Insert Into EmployeeRegion Values (1,"19713") 4> Insert Into EmployeeRegion Values (9,"48084") 5> Insert Into EmployeeRegion Values (9,"48304") 6> Insert Into EmployeeRegion Values (9,"55113") 7> Insert Into EmployeeRegion Values (9,"55439") 8> GO (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) (1 rows affected) 1> 2> CREATE FUNCTION dbo.udf_EmpRegionPlaceTAB ( 3> @EmployeeID int 4> ) RETURNS TABLE 5> AS RETURN 6> SELECT TOP 100 PERCENT WITH TIES 7> et.RegionID 8> , t.TerritoryDescription as [Territory] 9> , t.RegionID 10> FROM EmployeeRegion et 11> LEFT OUTER JOIN RegionPlace t 12> ON et.RegionID = t.RegionID 13> WHERE et.EmployeeID = @EmployeeID 14> ORDER BY t.TerritoryDescription 15> 16> GO 1> 2> DECLARE @EmpID int 3> SELECT @EmpID = EmployeeID 4> FROM Employees 5> WHERE FirstName = "Andrew" and LastName = "Fuller" 6> 7> SELECT * FROM udf_EmpRegionPlaceTAB(@EmpID) 8> GO RegionID Territory RegionID


-------------------------------------------------- -----------

(0 rows affected) 1> 2> 3> 4> drop FUNCTION dbo.udf_EmpRegionPlaceTAB; 5> GO 1> drop table EmployeeRegion; 2> drop table RegionPlace; 3> drop table Employees; 4> GO 1>

</source>