1 / 4

Parametre og variable i T-SQL

This document outlines the creation and execution of T-SQL stored procedures within the AdventureWorks database. It covers the `uspGetEmployees`, which retrieves employee details based on first and last names, and the `uspGetList`, which lists products under a specific subcategory with a price filter. Output parameters are utilized effectively to return max list price and comparison values. The examples provided demonstrate how to use these procedures, ensuring efficient data handling for employee and product information.

maegan
Télécharger la présentation

Parametre og variable i T-SQL

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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

  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 • 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;

More Related