将SQL表结果存储在变量中 [英] Store SQL table result within a variable

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

问题描述

您好,



我想将一个select语句的结果存储在一个变量中,以便我可以将该变量用作另一个变量的输入。 br $> b $ b

我尝试过的事情:



您好过以下了/>


set @columnname ='select top 1来自aud的PrimaryKey tablename ='+''''+ @TableName +''''+'和PrimaryKey ='+ @ PrimaryKeyValue

exec(@columnname)

解决方案

试试这个

 < span class =code-keyword>声明  @ columnname   varchar  50 
set @ columnname =(< span class =code-keyword>选择 top 1 MyColumn MyTable 其中 .....)
print @ COLUMNNAME


Hi There,

I would like to store the result of a select statement within a variable so that I can use the variable as input to another variable.

What I have tried:

Hi have tried the following

set @columnname = 'select top 1 PrimaryKey from aud where tablename =' + '''' + @TableName + '''' + 'and PrimaryKey = ' + @PrimaryKeyValue
exec (@columnname)

解决方案

try this

declare @columnname varchar(50)
set @columnname  = ( select top 1  MyColumn from MyTable where .....  )
print @columnname


这篇关于将SQL表结果存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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