I want to store the result of this sql query in variable #a. The result contains 17 rows. How to edit this code in order to store the rows in #a?
declare #a uniqueidentifier
select EnrollmentID into #a from Enrollment
You cannot store 17 values inside a scalar variable. You can use a table variable instead.
This is how you can declare it:
DECLARE #a TABLE (id uniqueidentifier)
and how you can populate it with values from Enrollment table:
INSERT INTO #a
SELECT EnrollmentID FROM Enrollment
You should declare #a as a Table Variable with one column of type unique identifier as follows:
DECLARE #a TABLE (uniqueId uniqueidentifier);
INSERT INTO #a
SELECT EnrollmentID
FROM Enrollment;
Related
I have a variable called result of type Xml that contains one cell of Xml text. I cannot seem to use SELECT INTO to insert this value into another temp table.
SELECT #result
INTO dbo.xml_temp
Is there a way to achieve this?
If you want to insert the XML into an existing table, you have
VALUES
or INSERT INTO ... SELECT ...:
Try this:
DECLARE #tbl TABLE(ID INT IDENTITY,TargetColumn XML);
DECLARE #SomeXML XML ='<root>test</root>';
INSERT INTO #tbl VALUES(#SomeXML);
INSERT INTO #tbl(TargetColumn) SELECT #SomeXML;
SELECT * FROM #tbl;
If you really want to create a new temp table, your statement is just missing an alias (How should the new table know the column's name?):
SELECT #SomeXML AS SomeName INTO #tmpTable;
SELECT * FROM #tmpTable;
I want to know if it is possible to insert to a table from a specific column of result from a stored procedure?
Something like:
declare #temp as table(
id int
)
insert #temp
exec getlistofStudents --returns multiple columns
this is an example only, Thanks for the help..
You can take a 2 step approach. First INSERT INTO a #TempTable, then populate the #TempVariable with another INSERT INTO, selecting the single column.
DECLARE #temp AS TABLE
(
ID int
);
CREATE TABLE #tempTable1
(
Column1 int,
Column2 int
);
INSERT INTO #tempTable1
Exec getlistofStudents
INSERT INTO #temp
SELECT Column1 FROM #tempTable1
I want to write a stored procedure to loop through the table which has membership id and person id details and pass it as variable to stored procedure.
Any thoughts on how to do it. Any help is highly appreciated.
DECLARE #membership NUMERIC(9)
DECLARE #person_id NUMERIC(9)
DECLARE #id_num INT
WHILE (
set #id_num=id_num+1
SELECT membership_id,person_id FROM [dbo].[reb] WHERE id_num <1160
EXEC p_get_details #membership_id, person_id
You have two options. first one is to pass two parameters to procedure p_get_details , each parameter being a comma separated string containing concatenated values of membership_id and person_id columns.
Other is to pass the whole resultset from SELECT query as a Table Valued Parameter to the procedure.
If you need to call the proc again and again for different table values then u can create a temp table which has all the membership and personids
See If this code works for you:-
DECLARE #membership_id NUMERIC(9)
DECLARE #person_id NUMERIC(9)
if(OBJECT_ID('tempdb..#tempids') is not null)
drop table #tempids
SELECT membership_id,person_id
into #tempids
FROM [dbo].[reb]
WHERE id_num <1160
while exists(select 1 from #tempids)
begin
-- select 1 row which will be called by proc
select top 1 #membership_id=membership_id
#person_id =person_id
from #tempids
EXEC p_get_details #membership_id, #person_id
--delete the ids which have been called by proc
delete from #tempids
where
membership_id=#membership_id
person_id =#person_id
end
When I want to get last inserted ID to a table I use it like this:
insert into table1(col1,col2,col3)
values(val1,val2,val3)
declare #last_id int = scope_identity()
Now I have stored procedure that gets a value inserts multiple items:
insert into table1(col1,col2,col3)
select #val1,#val2,val3 from table2 where value=#value //#value is a single value that is passed to procedure as argument
now how can I get the list of IDs of these multiple rows that are added to table1?
declare #inserted table (Id int)
insert into table1(col1,col2,col3)
output inserted.Id into #inserted (Id)
select #val1,#val2,val3
from table2
where value=#value
select * from #inserted
The SCOPE_IDENTITY() gets you the last generated Identity Value in the identity column. to get all the newly inserted values you will need to make use of OUTPUT clause along with a table variable. See below how you can do that.
DECLARE #New_IDs TABLE(IDs INT)
insert into table1(col1,col2,col3)
OUTPUT inserted.Id_column INTO #New_IDs
select #val1,#val2,val3
from table2
where value=#value
SELECT * FROM #New_IDs
My query in Stored procedure looks like this.
insert into a(name) select name from b.
I want to get all the new identity value from a.
How can i do this?
Thanks in advance.
insert into a(name)
OUTPUT INSERTED.IDColumn --this line for >1 row
select name from b.
The OUTPUT Clause
Have a look at OUTPUT Clause (Transact-SQL)
Something like
DECLARE #a TABLE(
ID INT IDENTITY (1,1),
Name VARCHAR(250)
)
DECLARE #b TABLE(
Name VARCHAR(250)
)
INSERT INTO #b SELECT 'a'
INSERT INTO #b SELECT 'b'
INSERT INTO #b SELECT 'c'
INSERT INTO #b SELECT 'd'
INSERT INTO #a(name)
OUTPUT INSERTED.ID
SELECT name FROM #b