由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Database版 - SQL Server - delcare variable dynamically
相关主题
Question about T-SQLerror of executing SQL query of string concatenation (转载
请教一个有关SQL concat的问题一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ... (转载)
SQL Server 2005 Exec( )SQL Server - how to obtain data type name
这个query怎么写?SQL, recruiter发过来的面试题 (转载)
SQL请教...怎么写这样的distribution list请教set和select 的区别
T-SQL 问题A question about recursive query
求助:如何ColumName 作为变量放入query请各位帮我看看这个最简单的Stored Procedure (转载)
问一个SQL Server的问题question about import xml to sql table
相关话题的讨论汇总
话题: col话题: data话题: declare话题: type话题: variable
进入Database版参与讨论
1 (共1页)
y****9
发帖数: 144
1
I have a need to declare variable dynamically, i.e. variable name and type
will be known at run time; I am trying to use sp_executesql. But the
following test failed:
use testdata
go
exec sp_executesql 'declare @col_1 int' --failed
-- declare @col_1 int -- this is ok
set @col_1=1
print @col_1
Any idea why it fails or any suggestions on how to do declaring variable
dynamically?
For example, I want to do:
declare @col_1 int
declare @col_2 varchar(10)
declare @col_3 varchar(100)
but the number of variable and type will be know at run time.
Thanks
s**********0
发帖数: 266
2
declare @sqlcmd varchar(8000)
set @sqlcmd = 'declare @col_1 int; set @col_1 = 3; print @col_1'
exec (@sqlcmd)
y****9
发帖数: 144
3

Thank you for your input, but it seems not what I want. basically I am doing:
fetch next from mycur
into @variable1,@variable2 ...
However I need to build the variable list dynamically.
The whole procedure looks like this:
1. pass a query text to a procedure (like: select * from sys.objects)
2. In this procedure I open a cursor for the query
3. I need to process this cursor row by row
4. for each row, i want to refer to each column to do sth on the column.
You can see the column names will be only known at run time. For this I can
get info about column name and type from sp_describe_cursor_columns
Now I stuck with step 3 as the fetch has to know the @variable. I build the
declare statements, but seems run it with sp_executesql does not help.
BTW, there is no application requirement. I just want to accomplish above
task as a way to learn T-SQL a little bit.

【在 s**********0 的大作中提到】
: declare @sqlcmd varchar(8000)
: set @sqlcmd = 'declare @col_1 int; set @col_1 = 3; print @col_1'
: exec (@sqlcmd)

i****a
发帖数: 36252
4
LS has the correct info.
you need to construct your @sqlcmd fully before you execute.
do "print @sqlcmd", copy the result and execute it in new window

doing:
can

【在 y****9 的大作中提到】
:
: Thank you for your input, but it seems not what I want. basically I am doing:
: fetch next from mycur
: into @variable1,@variable2 ...
: However I need to build the variable list dynamically.
: The whole procedure looks like this:
: 1. pass a query text to a procedure (like: select * from sys.objects)
: 2. In this procedure I open a cursor for the query
: 3. I need to process this cursor row by row
: 4. for each row, i want to refer to each column to do sth on the column.

y****9
发帖数: 144
5

Thanks for the above hint, it helps. I am finally close to my goal. As an
Oracle DBA, I am used to use command line. What I try to do is to simulate
Tom kyte's print rows as colum procedure in Oracle.
I did two test cases, usp_p is my procedure which is to display rows as
column for an input sql.
1> exec usp_p @s='select top 5 id, clustered_data, scattered_data from big_
table'
2> go
----------------------------------------------
id: 1
clustered_data: 0
scattered_data: 0
----------------------------------------------
id: 2
clustered_data: 0
scattered_data: 1
----------------------------------------------
id: 3
clustered_data: 0
scattered_data: 2
----------------------------------------------
id: 4
clustered_data: 0
scattered_data: 3
----------------------------------------------
id: 5
clustered_data: 0
scattered_data: 4
----------------------------------------------
1> exec usp_p @s='select top 5 name, object_id, type_desc from sys.objects'
2> go
----------------------------------------------
name: sysrscols
object_id: 3
type_desc: S
----------------------------------------------
name: sysrowsets
object_id: 5
type_desc: S
----------------------------------------------
name: sysallocunits
object_id: 7
type_desc: S
----------------------------------------------
name: sysfiles1
object_id: 8
type_desc: S
----------------------------------------------
name: syspriorities
object_id: 17
type_desc: S
----------------------------------------------
The second test case show the type_desc is not displayed correctly, I will
try to fix later. Once I fix all the data type, I will publish the code in
my blog if anyone is insterested to look at the code.

【在 i****a 的大作中提到】
: LS has the correct info.
: you need to construct your @sqlcmd fully before you execute.
: do "print @sqlcmd", copy the result and execute it in new window
:
: doing:
: can

y****9
发帖数: 144
6
The procedure source code can be found from my new blog post:
http://oracle-study-notes.blogspot.com/2011/09/sql-server-proce
g***l
发帖数: 18555
7
这个就是DYNAMIC SQL吧
1 (共1页)
进入Database版参与讨论
相关主题
question about import xml to sql tableSQL请教...怎么写这样的distribution list
请问sql server里面怎么输出变量到文本文件?T-SQL 问题
pass parameter from stored procedure to SSIS求助:如何ColumName 作为变量放入query
SQL Server 如何把日期自动加入文件名中?问一个SQL Server的问题
Question about T-SQLerror of executing SQL query of string concatenation (转载
请教一个有关SQL concat的问题一个sql问题:怎样实现 (((a1*10)+a2)*10+a3)*10 ... (转载)
SQL Server 2005 Exec( )SQL Server - how to obtain data type name
这个query怎么写?SQL, recruiter发过来的面试题 (转载)
相关话题的讨论汇总
话题: col话题: data话题: declare话题: type话题: variable