April 28, 2009 at 9:00 am
hi all,
i have two tables, one table is contains all tables names,other table contains all columns related to the tablename.
i want to use the second table to get the required columns from first table.
if i use the sp, i can do it. but i want it from query
for example::
select tablename from secondtable;
select column1+'-'+column2 from tablename;( here table name is from above query).
please help me in this issue..
thanks for the help
Rock....
April 28, 2009 at 10:02 am
Please post the definitions of the two tables i.e. Create statements, your queries and the results you expect.
Thanks
Greg
April 28, 2009 at 10:11 am
a typical example from SQL server's metadata:
SELECT
sysobjects.name As TableName ,
syscolumns.name As ColumnName
from sysobjects
inner join syscolumns on sysobjects.id = syscolumns.id
where sysobjects.xtype='U'
order by syscolumns.name,syscolumns.colid
Lowell
April 28, 2009 at 10:51 pm
hi
i know this query, actually the problem is, in the first table the columns are automatically generated from the application and also their fields on other table. i think now you got my point
thank you for the help
ROCK..
April 28, 2009 at 11:21 pm
Hi,
Can you please elaborate your question or post your table structure here.
April 28, 2009 at 11:55 pm
select tablename from secondtable;
select column1+'-'+column2 from tablename;( here table name is from above query).
You can take the result of first query into a variable (@tbname).
Make dynamic query for the second one. Replace the tablename naem with the above variable(@tbname).
April 29, 2009 at 2:14 am
hi yesterday i did that using the varialbe and dynimically executed. i got it..
i use the below query
declare @var1 varcahr(300)
set @var1=tablename from firsttable;
exec('select * from'+@var1')
this works
but my requirement is
like this:
declare @var1 varcahr(300)
set @var1=tablename from firsttable;
exec('select column1'+'_'+'column2 from'+@var1')
(here column1 is the firstname and column2 is the lastname)
this gives an errror.
incorrect sysntax near '_'
can anyone help me on this
thankyou
ROCK...
April 29, 2009 at 3:06 am
but my requirement is
like this:
declare @var1 varcahr(300)
set @var1=tablename from firsttable;
exec('select column1'+'_'+'column2 from'+@var1')
try the below query...
declare @var1 varcahr(300)
set @var1=tablename from firsttable;
declare @qry nVarchar(500)
Set @qry='select column1'+'_'+'column2 from'+@var1'
exec(@qry)
by the way it's SQL Server 2000 or 2005 ?
April 30, 2009 at 7:50 am
hi
same error no change in that
i am using sqlserver 2005
thanks
ROCK.
April 30, 2009 at 11:12 am
Did you want this?:
Set @qry='select column1 + ''_'' + column2 from '+@var1
Where '' is two consecutive single quotes.
April 30, 2009 at 12:21 pm
Hi Rock
I'm not completely sure if I got you, but I think you want to get this result from your dynamic SQL:
SELECT column1 + '_' + column2 FROM YourTableFromOtherTable
So you have to add the ' to your dynamic part (masked by doubled):
declare @var1 varcahr(300)
set @var1=tablename from firsttable;
exec('select column1+''_''+column2 from '+@var1)
Tip: To create dynamic SQL use PRINT for developing 😉
Greets
Flo
April 30, 2009 at 10:23 pm
HI
thank you very much this works fine..
ROCK...
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply