为了账号安全,请及时绑定邮箱和手机立即绑定

将动态SQL的结果转换为sql-server的变量

将动态SQL的结果转换为sql-server的变量

繁华开满天机 2019-12-11 13:09:42
在存储过程中执行动态SQL,如下所示:DECLARE @sqlCommand nvarchar(1000)DECLARE @city varchar(75)SET @city = 'London'SET @sqlCommand = 'SELECT COUNT(*) FROM customers WHERE City = @city'EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75)', @city = @city如何在SP中使用count(*)列值作为返回值?
查看完整描述

3 回答

?
30秒到达战场

TA贡献1828条经验 获得超6个赞

DECLARE @sqlCommand nvarchar(1000)

DECLARE @city varchar(75)

declare @counts int

SET @city = 'New York'

SET @sqlCommand = 'SELECT @cnt=COUNT(*) FROM customers WHERE City = @city'

EXECUTE sp_executesql @sqlCommand, N'@city nvarchar(75),@cnt int OUTPUT', @city = @city, @cnt=@counts OUTPUT

select @counts as Counts



查看完整回答
反对 回复 2019-12-12
?
浮云间

TA贡献1829条经验 获得超3个赞

您可能已经尝试过了,但是您的规格是否可以执行此操作?


DECLARE @city varchar(75)

DECLARE @count INT

SET @city = 'London'

SELECT @count = COUNT(*) FROM customers WHERE City = @city



查看完整回答
反对 回复 2019-12-12
?
BIG阳

TA贡献1859条经验 获得超6个赞

动态版本


    ALTER PROCEDURE [dbo].[ReseedTableIdentityCol](@p_table varchar(max))-- RETURNS int

    AS

    BEGIN

        -- Declare the return variable here

       DECLARE @sqlCommand nvarchar(1000)

       DECLARE @maxVal INT

       set @sqlCommand = 'SELECT @maxVal = ISNULL(max(ID),0)+1 from '+@p_table

       EXECUTE sp_executesql @sqlCommand, N'@maxVal int OUTPUT',@maxVal=@maxVal OUTPUT

       DBCC CHECKIDENT(@p_table, RESEED, @maxVal)

    END



exec dbo.ReseedTableIdentityCol @p_table='Junk'



查看完整回答
反对 回复 2019-12-12
  • 3 回答
  • 0 关注
  • 499 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信