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;
Related
I have a Database and i want to execute few queries on it, and the results of the queries i.e. message, has to be comapared in my code if it is expected or not.
Please let me know how to capture the output of the SQL queries in any variable that can be used later in code for comparision.
Try the following Method :
Create a Table with the Same structure as of your Procedure output
Insert the Result of the SP Execution to the Table
Compare your Query result with the Table
Like this
CREATE PROCEDURE dbo.uSp_Temp
AS
SELECT
GETDATE() "MyDate"
DECLARE #T TABLE
(
MyDate DATE
)
INSERT INTO #T
EXEC uSp_Temp
SELECT
*
FROM #T
You can use Temp tables or temp variable to save the result set of a query.
Below is the sample for temp table
create table #temp (id int)
insert into #temp
select 1 as id
select * from #temp
Below is sample for Temp variable
declare #temp table (id int)
insert into #temp
select 1 as id
select * from #temp
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;
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
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
I have a database table with a column METADATA. This METADATA may or may not contain a string. Here's the sample string:
StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5
Now, I want to extract the StudentNickName:TestName if it exists. Please note of the following constraint:
METADATA column doesn't always contain a value
METADATA column can contain a value without the StudentNickName clause
The StudentNickName: is fixed, while the TestNName varies per row.
For mssql 2005+
declare #t table(metadata varchar(200))
insert #t values('StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5')
insert #t values('')
insert #t values('StudentID:1234,StudentName:Thomas,StudentNickName:Tom,StudentLevel:3')
select left(b.a, patindex('%_,%', b.a)) StudentNickName
from #t t cross apply
(select right(metadata, patindex('%_:emaNkciNtnedutS%'
, reverse('X'+ metadata)))+',' a) b
Result:
StudentNickName
---------------
TestNName
Tom
Works in Sql Server
DECLARE #test TABLE(metadata VARCHAR(200))
INSERT #test VALUES('StudentID:1234,StudentName:TestName,StudentNickName:TestNName,StudentLevel:5')
INSERT #test VALUES('StudentID:1235,StudentName:TestName1,StudentNickName:TestNName1,StudentLevel:6')
INSERT #test VALUES('StudentID:1236,StudentName:TestName2,StudentNickName:TestNName2,StudentLevel:2')
INSERT #test VALUES('')
SELECT split.s.value('.','VARCHAR(100)') as colname FROM
(
SELECT CAST('<s>' + REPLACE(metadata,',','</s><s>') + '</s>' AS XML) col FROM #test
) AS t CROSS APPLY col.nodes('/s') AS split(s)
WHERE split.s.value('.','VARCHAR(100)') LIKE '%StudentNickName%'