从变量拉入临时表 [英] Pulling from variable into temp table
问题描述
这是整个项目的一个片段。我正在尝试做的是选择@Begin执行@Begin,然后将While循环的结果放入#While_Loop temptable,但我不断提出没有任何东西被放入Temp或只是@begin被放入的字符串温度基本上我只需要帮助'插入'部分的语法
声明@TTWhile varchar(30)
如果@ProgID< ;> 0选择@TTwhile ='#temp_prog_course_list_GR'
如果@ConcID<> 0选择@TTWhile ='#temp_prog_course_list_UG'
声明@Begin nvarchar(max)
选择@Begin ='
声明@ctr int,@ max int,@ Codecode varchar(25),@ Coursecode2 varchar(50), @courseCodeList varchar(max),@ CourseCodeList2 varchar(max)
set @ctr = 1
选择@max = max(RecordID)从'+ @TTWhile +'
虽然@ctr< = @max
开始
选择@CourseCode = ltrim(rtrim(courseCode))
来自'+ @TTwhile +'其中RecordID = @ctr
选择@courseCodeList = coalesce(@courseCodeList +'',''+ @CourseCode,@ CourseCode)
选择@ CourseCode2 =''max(''+ ltrim(rtrim(courseCode))+'')[''+ ltrim(rtrim(courseCode))+' ''''
来自'+ @TT while +'其中RecordID = @ctr
选择@ courseCodeList2 = coalesce(@ courseCodeList2 +'',''+ @ courseCode2,@ courseCode2)
选择@ctr = @ctr + 1
结束'
exec(@begin)
创建表#While_Loop
(cc1 nvarchar(max),
cc2 nvarchar(max))
插入# While_Loop - 选择(cc1,cc2)
来自@begin
声明@ cc1 varchar(最大值) ),@ cc2 varchar(max)
select @ cc1 =(从#While_Loop选择cc1)
选择@ cc2 =(从#While_Loop选择cc2)
我尝试过:
select * into
插入... exec
等...
< blockquote>尽管我不喜欢只是抛出代码。
插入#While_Loop(@courseCodeList,@ courseCodeList2)
您执行varchar @begin作为sql脚本,因此您在其中创建的变量存在于您运行它的环境中。它们在运行后可以自行使用,而不是作为varchar名称引用的某些微环境的一部分。
您应该重构整个@begin业务到存储过程中,返回您感兴趣的2个变量作为输出参数。
This is a snipbit of the entire project. What I'm trying to do is select @Begin execute @Begin and then put those results of the While loop into the #While_Loop temptable but I keep coming up with nothing being put into the Temp or just the string that @begin getting put into the Temp. Essentially I just need help with the Syntax of the 'Insert into' section
Declare @TTWhile varchar(30)
If @ProgID <> 0 Select @TTwhile = '#temp_prog_course_list_GR'
If @ConcID <> 0 Select @TTWhile = '#temp_prog_course_list_UG'
declare @Begin nvarchar(max)
Select @Begin = '
declare @ctr int, @max int, @Coursecode varchar(25), @Coursecode2 varchar(50), @courseCodeList varchar(max), @CourseCodeList2 varchar(max)
set @ctr = 1
Select @max = max(RecordID) From ' + @TTWhile + '
While @ctr <= @max
Begin
Select @CourseCode = ltrim(rtrim(courseCode))
From ' + @TTwhile + ' where RecordID = @ctr
Select @courseCodeList = coalesce(@courseCodeList + '','' + @CourseCode, @CourseCode)
Select @CourseCode2 = '' max(''+ltrim(rtrim(courseCode))+'') as [''+ltrim(rtrim(courseCode))+'']''
From '+ @TTwhile + ' where RecordID = @ctr
Select @courseCodeList2 = coalesce(@courseCodeList2 + '','' + @courseCode2, @courseCode2)
Select @ctr = @ctr + 1
End'
exec (@begin)
Create Table #While_Loop
(cc1 nvarchar(max),
cc2 nvarchar(max))
Insert into #While_Loop --select (cc1, cc2)
From @begin
Declare @cc1 varchar(max), @cc2 varchar(max)
select @cc1 = (Select cc1 From #While_Loop)
select @cc2 = (Select cc2 From #While_Loop)
What I have tried:
select * into
Insert into ... exec
etc...
As much as I dislike just throwing code out there.
Insert into #While_Loop (@courseCodeList, @courseCodeList2)
You execute the varchar @begin as a sql script, so the variables you create inside it exist in the environment you ran it from. They are availible on their own after it is run, not as part of some micro-environment referenced by the varchar's name.
You should probably refactor that whole @begin business into a stored procedure that return the 2 variables you are interested in as output parameters.
这篇关于从变量拉入临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!