Presentation is loading. Please wait.

Presentation is loading. Please wait.

Parametre og variable i T-SQL 1.Parametre (input) 2.Parametre (output) 3.Variable.

Similar presentations


Presentation on theme: "Parametre og variable i T-SQL 1.Parametre (input) 2.Parametre (output) 3.Variable."— Presentation transcript:

1 Parametre og variable i T-SQL 1.Parametre (input) 2.Parametre (output) 3.Variable

2 Parametre (Input) 1.USE AdventureWorks; 2.GO 3.IF OBJECT_ID ( 'HumanResources.uspGetEmployees', 'P' ) IS NOT NULL 4.DROP PROCEDURE HumanResources.uspGetEmployees; 5.GO 6.CREATE PROCEDURE HumanResources.uspGetEmployees 7.@LastName nvarchar(50), 8.@FirstName nvarchar(50) 9.AS 10.SET NOCOUNT ON; 11.SELECT FirstName, LastName, JobTitle, Department 12.FROM HumanResources.vEmployeeDepartment 13.WHERE FirstName = @FirstName AND LastName = @LastName; 14.GO 1.EXECUTE HumanResources.uspGetEmployees N'Ackerman', N'Pilar'; 2. -- Or 3.EXEC HumanResources.uspGetEmployees @LastName = N'Ackerman', @FirstName = N'Pilar'; 4.GO 5.-- Or 6.EXECUTE HumanResources.uspGetEmployees @FirstName = N'Pilar', @LastName = N'Ackerman'; 7.GO

3 Parametre (Output) USE AdventureWorks; GO IF OBJECT_ID ( 'Production.uspGetList', 'P' ) IS NOT NULL DROP PROCEDURE Production.uspGetList; GO CREATE PROCEDURE Production.uspGetList @Product varchar(40), @MaxPrice money, @ComparePrice money OUTPUT, @ListPrice money OUT AS SET NOCOUNT ON; SELECT p.[Name] AS Product, p.ListPrice AS 'List Price' FROM Production.Product AS p JOIN Production.ProductSubcategory AS s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.[Name] LIKE @Product AND p.ListPrice < @MaxPrice; -- Populate the output variable @ListPprice. SET @ListPrice = (SELECT MAX(p.ListPrice) FROM Production.Product AS p JOIN Production.ProductSubcategory AS s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.[Name] LIKE @Product AND p.ListPrice < @MaxPrice); -- Populate the output variable @compareprice. SET @ComparePrice = @MaxPrice; GO

4 Variable 1.USE AdventureWorks; 2.GO SET NOCOUNT ON; 3.GO DECLARE @Group nvarchar(50), @Sales money; 4.SET @Group = N'North America'; 5.SET @Sales = 2000000; 6.SET NOCOUNT OFF; 7.SELECT FirstName, LastName, SalesYTD 8.FROM Sales.vSalesPerson 9.WHERE TerritoryGroup = @Group and SalesYTD >= @Sales; 1.USE AdventureWorks; 2.GO DECLARE @find varchar(30); 3./* Also allowed: 4.DECLARE @find varchar(30) = 'Man%'; 5.*/ 6.SET @find = 'Man%'; 7.SELECT LastName, FirstName, Phone 8.FROM Person.Contact WHERE LastName LIKE @find;


Download ppt "Parametre og variable i T-SQL 1.Parametre (input) 2.Parametre (output) 3.Variable."

Similar presentations


Ads by Google