November 24, 2010 at 8:49 pm
hi,
I have write the query like
-----------------------------------------
insert into tbl_xml(EmpId,EmpName)
SELECT
colx.value('id[1]','int') AS EmpId,
colx.value('name[1]','VARCHAR(max)') AS EmpName
FROM @data.nodes('dataSet/Items') as tabx(colx)
---------------------------------------------------------------------
It works fine.
But I am trying to write this same query like it gives an error
query is
------------------------------------------------------
declare @Cmd varchar(max);
set @Cmd='insert into tbl_xml(EmpId,EmpName)
SELECT
colx.query(''data(id[1]'',''int'') as EmpId,
colx.query(''data(name[1])'',''varchar'') as EmpName
FROM @data.nodes(''dataSet/Items'') as tabx(colx)'
exec(@Cmd);
------------------------------------------------------------
November 24, 2010 at 8:53 pm
kuppurajm (11/24/2010)
hi,I have write the query like
-----------------------------------------
insert into tbl_xml(EmpId,EmpName)
SELECT
colx.value('id[1]','int') AS EmpId,
colx.value('name[1]','VARCHAR(max)') AS EmpName
--cast(colx.query('data(id) ') as varchar) as EmpId,
--cast(colx.query('data(name) ') as varchar) as EmpName
FROM @data.nodes('dataSet/Items') as tabx(colx)
---------------------------------------------------------------------
It works fine.
But I am trying to write this same query like it gives an error
query is
------------------------------------------------------
declare @Cmd varchar(max);
set @Cmd='insert into tbl_xml(EmpId,EmpName)
SELECT
colx.query(''data(id[1]'',''int'') as EmpId,
colx.query(''data(name[1])'',''varchar'') as EmpName
FROM @data.nodes(''dataSet/Items'') as tabx(colx)'
exec(@Cmd);
------------------------------------------------------------
1. What error is it giving you?
2. The dynamic sql is not the same as the query that works. Specifically, in the dynamic you are using colx.query; in the regular you are using colx.value. (The regular query has the .query stuff remarked out.)
3. You don't need dynamic sql for the query shown - why are you bothering with it?
Wayne
Microsoft Certified Master: SQL Server 2008
Author - SQL Server T-SQL Recipes
November 24, 2010 at 9:15 pm
because I need to specify columns name as variable dynamically
so that I am expecting this to run with exec
now I am using value fun in both of the cases but it give same error
------------------------------
SELECT
colx.value('id[1]','int') AS EmpId,
colx.value('name[1]','VARCHAR(max)') AS EmpName
FROM @data.nodes('dataSet/Items') as tabx(colx)
declare @Cmd varchar(max);
set @Cmd='SELECT
colx.value(''id[1]'',''int'') as EmpId,
colx.value(''name[1])'',''varchar'') as EmpName
FROM @data.nodes(''dataSet/Items'') as tabx(colx)'
exec(@Cmd);
November 24, 2010 at 10:23 pm
the problem here looks like that @data variable is not in the scope when you execute @Cmd. you need to declare @data like
declare @Cmd varchar(max);
set @cmd = 'declare @data <.......compelte declaration and set the variable with expected values.....> '
set @Cmd=@Cmd + 'SELECT
colx.value(''id[1]'',''int'') as EmpId,
colx.value(''name[1])'',''varchar'') as EmpName
FROM @data.nodes(''dataSet/Items'') as tabx(colx)'
exec(@Cmd);
November 24, 2010 at 10:36 pm
Thank you very much
now it works fine.
but my requirement is I want to specify column name and data type dynamicaly
how to specify dynamically with this query
------------------------------------------
declare @sql varchar(max)
set @sql='declare @data xml'
set @sql=@sql + ' set @data=''<dataSet>
<Items>
<id>0</id>
<name>name0</name>
</Items>
<Items>
<id>1</id>
<name>name1</name>
</Items>
<Items>
<id>2</id>
<name>name2</name>
</Items>
</dataSet>'''
set @sql=@sql + 'SELECT
colx.value(''id[1]'',''int'') AS EmpId,
colx.value(''name[1]'',''VARCHAR(max)'') AS EmpName
FROM @data.nodes(''dataSet/Items'') as x(colx)'
print(@sql)
exec(@sql)
----------------------------------------------------
This query works fine
but instead of ''id[1]'',''int'' I need to use variable
November 24, 2010 at 10:38 pm
November 24, 2010 at 11:05 pm
Hi,
You need to declare two input parameters for each column
1) columnname
2) datatype.
so that u can dynamically declare them.
November 26, 2010 at 7:20 am
You can try something like this for concat FieldName and/or types to a query.
DECLARE @Field2 AS VARCHAR(50)
SET @Field2='FieldTwo'
DECLARE @Field3 AS VARCHAR(50)
SET @Field3='''ID[0]'''
DECLARE @Type3 AS VARCHAR(50)
SET @Type3='''Int'''
DECLARE @SQL AS VARCHAR(100)
SET @SQL='Select '+@Field1+','+@Field2+', cox.value('+@Field3+','+@Type3+') from ATable'
print(@SQL)
The output os this query was:
Select 'FieldOne',FieldTwo, cox.value('ID[0]','Int') from ATable
I'm pretty sure you already know this, but if you have the field names and types as parameters there is no need to declare the vars.
Hope it helps.
November 26, 2010 at 7:22 am
sorry i did not copy the first two statemetens. Here we go again.
DECLARE @Field1 AS VARCHAR(50)
SET @Field1='''FieldOne'''
DECLARE @Field2 AS VARCHAR(50)
SET @Field2='FieldTwo'
DECLARE @Field3 AS VARCHAR(50)
SET @Field3='''ID[0]'''
DECLARE @Type3 AS VARCHAR(50)
SET @Type3='''Int'''
DECLARE @SQL AS VARCHAR(100)
SET @SQL='Select '+@Field1+','+@Field2+', cox.value('+@Field3+','+@Type3+') from ATable'
print(@SQL)
output:
Select 'FieldOne',FieldTwo, cox.value('ID[0]','Int') from ATable
November 26, 2010 at 7:25 am
Im miss the first part sorry.
DECLARE @Field1 AS VARCHAR(50)
SET @Field1='''FieldOne'''
DECLARE @Field2 AS VARCHAR(50)
SET @Field2='FieldTwo'
DECLARE @Field3 AS VARCHAR(50)
SET @Field3='''ID[0]'''
DECLARE @Type3 AS VARCHAR(50)
SET @Type3='''Int'''
DECLARE @SQL AS VARCHAR(100)
SET @SQL='Select '+@Field1+','+@Field2+', cox.value('+@Field3+','+@Type3+') from ATable'
print(@SQL)
output:
Select 'FieldOne',FieldTwo, cox.value('ID[0]','Int') from ATable
November 26, 2010 at 11:13 am
jonetiger (11/24/2010)
I can not find it, because I am new about it. I give you ans come back later.
Freakin' low life spammer. Spam reported.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply