473,382 Members | 1,392 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

store result of query into a variable

i need to put the result of quey @st into a variable. could anyone please tell me how to do that?


select @sys= + columnName+', ' from tab
set @sys=left(@sys, len(@sys)-1)
select @sys

DECLARE cursor_insert CURSOR FOR
SELECT insequenceid FROM temp_ac
OPEN cursor_insert
FETCH NEXT FROM cursor_Insert into @inseq

WHILE @@FETCH_STATUS = 0
BEGIN
set @st= ('select ' + @sys + ' from temp_ac ')
execute (@st)
FETCH NEXT FROM cursor_Insert into @inseq
END
CLOSE cursor_insert
DEALLOCATE cursor_insert

i want something so that @val= execute(@st)?????
Sep 12 '07 #1
3 11770
davef
98
Somethig like this will do:

declare @sql varchar(100),
@szDesc varchar(50)

--Create a test table and dump some data into it
create table Account (
nID int NOT NULL PRIMARY KEY,
szDesc varchar(50)
)
insert into Account
select 1, 'Primary'
insert into Account
select 2, 'Secondary'
insert into Account
select 3, 'Offshore'

--Declare a table variable corresponding in structure to Account table
declare @temp table (
nID int NOT NULL PRIMARY KEY,
szDesc varchar(50)
)
set @sql='select * from Account'
insert into @temp
exec (@sql)

--Store result of dynamic SQL query in a variable
select @szDesc=szDesc from @temp where szDesc like 'P%'
print @szDesc

--Drop the test table
drop table Account
Sep 12 '07 #2
this is not working.
i'm getting the error
"EXECUTE cannot be used as a source when inserting into a table variable"

moreover i cannot use temporary table as i dont kno the exact number of columns that are to be fetched as those columns will be selected on runtime only.
Sep 13 '07 #3
azimmer
200 Expert 100+
i need to put the result of quey @st into a variable. could anyone please tell me how to do that?


select @sys= + columnName+', ' from tab
set @sys=left(@sys, len(@sys)-1)
select @sys

DECLARE cursor_insert CURSOR FOR
SELECT insequenceid FROM temp_ac
OPEN cursor_insert
FETCH NEXT FROM cursor_Insert into @inseq

WHILE @@FETCH_STATUS = 0
BEGIN
set @st= ('select ' + @sys + ' from temp_ac ')
execute (@st)
FETCH NEXT FROM cursor_Insert into @inseq
END
CLOSE cursor_insert
DEALLOCATE cursor_insert

i want something so that @val= execute(@st)?????
With EXEC you open a new environment: that is you cannot directly move data in and out of the EXEC scope. If you can process @val within the EXEC scope (that is: it is not utterly complex or a return value) I suggest you define and process it within @st like this:
change this:
set @st= ('select ' + @sys + ' from temp_ac ')
to this:
set @st= ('select @val=' + @sys + ' from temp_ac ')
(append to your code):
Expand|Select|Wrap|Line Numbers
  1. set @st='declare @val varchar(255); '+@st
  2. set @st=@st+'; print @val;'    ' or whatever your processing is
  3.  
now EXEC(@st)

There can be tricky workarounds to this but if it works look no further.
Sep 13 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Adrian | last post by:
Sorry if this appears twice but 6 hours and wasn't up there! "Adrian" <Adrian@nospamhotmail.com.uk> wrote in message news:... > Hi > Is it possible and if so how to do the following? > > I...
1
by: Randy K | last post by:
I have a table with some 35000 records and I need some help sorting it out. The goal is to get counts of failures modes oraganized by serial number. the table is set up roughly like this. s/n ...
9
by: VMI | last post by:
I have two tables and I want to compare these two tables with a query( ie. "select * from A where B.key = A.key") and the result will be stored in a 3rd table (table C). is this possible? If...
4
by: Jim via DotNetMonster.com | last post by:
Hi, How can I assign the result of a function to a variable. I need to get the result so that I can query the database again. What I'm trying is:...
1
by: Timothy Wang | last post by:
Hi everyone : Is there anybody know how to use a string store in a variable as command ? Like : str1='test...' str2='...test' str3='left(str1, 5) & Right((str2,3 ) ' How do we get...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
1
by: TARUN | last post by:
Hello All I get stuck in one problem , Please help me and excuse me on the poor Knowledge in SQL Server Store procedure Q1...I want to return a string type value from store procedure , Is it...
6
by: Busbait | last post by:
I need to know how can I store SQL result into variable, I have used the below code but it dose not work. Is there an easier way to do this? ...
0
by: FLANDERS | last post by:
Hi all, Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this: UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.