如何在SQL Server中将变量保存到另一个变量中 [英] HOW TO SAVE PRINT VARIABLE INTO ANOTHER VARIALBE IN SQL SERVER

查看:102
本文介绍了如何在SQL Server中将变量保存到另一个变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我从net抓到的脚本

Hi
Here is my script grab from net

<br />
declare @col varchar(255), @cmd varchar(max)<br />
DECLARE getinfo cursor for<br />
SELECT c.name FROM sys.tables t JOIN sys.columns c ON t.Object_ID = c.Object_ID WHERE t.Name = ''tblDeductions'' AND c.name like ''fld%name''<br />
OPEN getinfo<br />
FETCH NEXT FROM getinfo into @col<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
    SELECT @cmd = ''IF EXISTS (SELECT top 1 * FROM tblDeductions WHERE ['' + @col + ''] IS NOT NULL) BEGIN print '''''' + @col + '''''' end''<br />
    exec (@cmd)<br />
    FETCH NEXT FROM getinfo into @col<br />
END<br />
CLOSE getinfo<br />
DEALLOCATE getinfo<br />



上面的查询将显示为空值的列名.但是我需要将它们保存到另一个用逗号分隔值的变量中.

在此先感谢.



The above query will display column names that are null values. But I need to save them into another variable with comma separated values.

Thanks in advance.

推荐答案

''HireMeForYourWork''吗? ROTFL !!!!

如果要保存它们,只需声明一个字符串变量并将其放在那里.如果要跳过空值,请先检查是否为空?您是说您在网上找到了此代码吗?这是否意味着您根本不了解?那我们为什么要雇用你来工作呢?您需要购买一些书籍并阅读,而不仅仅是复制随机代码并在需要更改以完成所需操作时碰壁.
''HireMeForYourWork'' ? ROTFL !!!!

If you want to save them, just declare a string variable and put them in there. If you want to skip nulls, check for null first ? Are you saying you found this code online ? Does that mean you don''t understand it at all ? Why would we hire you for our work then ? You need to buy some books and read them, not just copy random code and hit a wall when it needs changing to do what you want.


这篇关于如何在SQL Server中将变量保存到另一个变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆