sql根据传入参数确定选择前几条数据的存储过程

    技术2024-11-03  28

    sql代码:

    USE [BeijingAgricultureDB] GO DECLARE @return_value int EXEC @return_value = [dbo].[SelectTopNNotifications] @num = 2 SELECT 'Return Value' = @return_value GO

     

    其他示例:

    /* Show not having access to variables from the calling batch. */ DECLARE @CharVariable CHAR(3) SET @CharVariable = 'abc' /* sp_executesql fails because @CharVariable has gone out of scope. */ sp_executesql N'PRINT @CharVariable' GO /* Show database context resetting after sp_executesql completes. */ USE pubs GO sp_executesql N'USE Northwind' GO /* This statement fails because the database context has now returned to pubs. */ SELECT * FROM Shippers GO

     

    DECLARE @IntVariable INT DECLARE @SQLString NVARCHAR(500) /* Build and execute a string with one parameter value. */ SET @IntVariable = 35 SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' + CAST(@IntVariable AS NVARCHAR(10)) EXEC(@SQLString) /* Build and execute a string with a second parameter value. */ SET @IntVariable = 201 SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = ' + CAST(@IntVariable AS NVARCHAR(10)) EXEC(@SQLString)

     

    DECLARE @IntVariable INT DECLARE @SQLString NVARCHAR(500) DECLARE @ParmDefinition NVARCHAR(500) /* Build the SQL string once. */ SET @SQLString = N'SELECT * FROM pubs.dbo.employee WHERE job_lvl = @level' /* Specify the parameter format once. */ SET @ParmDefinition = N'@level tinyint' /* Execute the string with the first parameter value. */ SET @IntVariable = 35 EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable /* Execute the same string with the second parameter value. */ SET @IntVariable = 32 EXECUTE sp_executesql @SQLString, @ParmDefinition, @level = @IntVariable  

    最新回复(0)