Sei sulla pagina 1di 2

DROP PROCEDURE usp_GetSalesHistory GO CREATE PROCEDURE usp_GetSalesHistory ( @WhereClause NVARCHAR(2000) = NULL, @TotalRowsReturned INT OUTPUT ) AS BEGIN DECLARE @SelectStatement

NVARCHAR(2000) DECLARE @FullStatement NVARCHAR(4000) DECLARE @ParameterList NVARCHAR(500) SET @ParameterList = '@TotalRowsReturned INT OUTPUT' SET @SelectStatement = 'SELECT @TotalRowsReturned = COUNT(*) FROM SalesHistory ' SET @FullStatement = @SelectStatement + ISNULL(@WhereClause,'') PRINT @FullStatement EXECUTE sp_executesql @FullStatement, @ParameterList, @TotalRowsReturned = @TotalRowsReturned OUTPUT END GO In the above procedure, I need to declare a parameter list to pass into the sp_executesql stored procedure because a value is being assigned to the variable at run time. The only other change to the sp_executesql call is that I am assigning the output parameter from the call to the local @TotalRowsReturned parameter in my usp_GetSalesHistory stored procedure. I can even call my usp_GetSalesHistory stored procedure similar to the way I did before, but with the addition of an output parameter to indicate the rows that were returned. DECLARE @WhereClause NVARCHAR(2000), @TotalRowsReturned INT SET @WhereClause = 'WHERE Product = ''Computer''' EXECUTE usp_GetSalesHistory @WhereClause = @WhereClause, @TotalRowsReturned = @TotalRowsReturned OUTPUT SELECT @TotalRowsReturned

* Using EXECUTE Command */ /* Build and Execute a Transact-SQL String with a single parameter value Using EXECUTE Command */ /* Variable Declaration */ DECLARE @EmpID AS SMALLINT DECLARE @SQLQuery AS NVARCHAR(500) /* set the parameter value */ SET @EmpID = 1001

/* Build Transact-SQL String with parameter value */ SET @SQLQuery = 'SELECT * FROM tblEmployees WHERE EmployeeID = ' + CAST(@EmpID AS NVARCHAR(10)) /* Execute Transact-SQL String */ EXECUTE(@SQLQuery)

Example 1.1 Collapse | Copy Code /* Using sp_executesql */ /* Build and Execute a Transact-SQL String with a single parameter value Using sp_executesql Command */ /* Variable Declaration */ DECLARE @EmpID AS SMALLINT DECLARE @SQLQuery AS NVARCHAR(500) DECLARE @ParameterDefinition AS NVARCHAR(100) /* set the parameter value */ SET @EmpID = 1001 /* Build Transact-SQL String by including the parameter */ SET @SQLQuery = 'SELECT * FROM tblEmployees WHERE EmployeeID = @EmpID' /* Specify Parameter Format */ SET @ParameterDefinition = '@EmpID SMALLINT' /* Execute Transact-SQL String */ EXECUTE sp_executesql @SQLQuery, @ParameterDefinition, @EmpID 'SELECT emp_id, emp_name + '' '' + ''('' + CAST(COALESCE(emp_number,0) AS varchar(10)) + '')'' AS display_name FROM Employee ORDER BY emp_name' 'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') +' ')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name' 'SELECT emp_id, emp_name + '' '' + ''('' + CAST(COALESCE(emp_number,0) AS varchar(10)) + '')'' AS display_name FROM Employee ORDER BY emp_name' 'SELECT emp_id, emp_name + COALESCE(NULLIF(''('' + COALESCE(CAST(emp_number AS varchar(10)),'') +' ')'',''('' + '')''),'') AS display_name from Employee ORDER BY emp_name'