40 likes | 160 Views
Parametre og variable i T-SQL. Parametre (input) Parametre (output) Variable. Parametre (Input). USE AdventureWorks; GO IF OBJECT_ID ( 'HumanResources.uspGetEmployees', 'P' ) IS NOT NULL DROP PROCEDURE HumanResources.uspGetEmployees; GO
E N D
Parametre og variable i T-SQL Parametre (input) Parametre (output) Variable
Parametre (Input) • USE AdventureWorks; • GO • IF OBJECT_ID ( 'HumanResources.uspGetEmployees', 'P' ) IS NOT NULL • DROP PROCEDURE HumanResources.uspGetEmployees; • GO • CREATE PROCEDURE HumanResources.uspGetEmployees • @LastName nvarchar(50), • @FirstName nvarchar(50) • AS • SET NOCOUNT ON; • SELECT FirstName, LastName, JobTitle, Department • FROM HumanResources.vEmployeeDepartment • WHERE FirstName = @FirstName AND LastName = @LastName; • GO EXECUTE HumanResources.uspGetEmployeesN'Ackerman', N'Pilar'; -- Or EXEC HumanResources.uspGetEmployees @LastName = N'Ackerman', @FirstName = N'Pilar'; GO -- Or EXECUTE HumanResources.uspGetEmployees @FirstName = N'Pilar', @LastName = N'Ackerman'; GO
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
Variable • USE AdventureWorks; • GO SET NOCOUNT ON; • GO DECLARE @Group nvarchar(50), @Sales money; • SET @Group = N'North America'; • SET @Sales = 2000000; • SET NOCOUNT OFF; • SELECT FirstName, LastName, SalesYTD • FROM Sales.vSalesPerson • WHERE TerritoryGroup = @Group and SalesYTD >= @Sales; USE AdventureWorks; GO DECLARE @find varchar(30); /* Also allowed: DECLARE @find varchar(30) = 'Man%'; */ SET @find = 'Man%'; SELECT LastName, FirstName, Phone FROM Person.Contact WHERE LastName LIKE @find;