Get value from tables

  • 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....

  • Please post the definitions of the two tables i.e. Create statements, your queries and the results you expect.

    Thanks

    Greg

  • 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


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • 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..

  • Hi,

    Can you please elaborate your question or post your table structure here.

  • 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).

  • 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...

  • 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 ?

  • hi

    same error no change in that

    i am using sqlserver 2005

    thanks

    ROCK.

  • Did you want this?:

    Set @qry='select column1 + ''_'' + column2 from '+@var1

    Where '' is two consecutive single quotes.

  • 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

  • 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