Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003
    Location
    Arkansas
    Posts
    15

    Unanswered: @local variable (newbie)

    I am trying to run a query.

    Declare ColNames_Csr Cursor
    -- Open Cursor that contains all column names
    for
    Select Column_Name from Information_Schema.columns where Table_Name = @TableName

    Open ColNames_csr

    Fetch Next From ColNames_csr into @FieldName

    While ...

    Begin
    .
    .
    .
    Select @FieldValue = (Select @FieldName from Contacts)
    Print @FieldValue
    .
    .
    .
    end


    @SomeValue always returns the FieldName (EMail) not @FieldName (name@somewhere.com).

    I am trying to roll through all the columns and see what the value is for @fieldname

    What am i missing?

    Thanks
    LJ
    Hope is the feeling that the feeling that you have will not last very long.

  2. #2
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621

    Re: @local variable (newbie)

    Originally posted by LittleJonny
    I am trying to run a query.

    Declare ColNames_Csr Cursor
    -- Open Cursor that contains all column names
    for
    Select Column_Name from Information_Schema.columns where Table_Name = @TableName

    Open ColNames_csr

    Fetch Next From ColNames_csr into @FieldName

    While ...

    Begin
    .
    .
    .
    Select @FieldValue = (Select @FieldName from Contacts)
    Print @FieldValue
    .
    .
    .
    end


    @SomeValue always returns the FieldName (EMail) not @FieldName (name@somewhere.com).

    I am trying to roll through all the columns and see what the value is for @fieldname

    What am i missing?

    Thanks
    LJ
    Query below returns value of @FieldName - no field in table. Think about dynamic query ...

    (Select @FieldName from Contacts)

  3. #3
    Join Date
    Oct 2003
    Location
    Arkansas
    Posts
    15
    Dynamic Query? Im not sure I understand.

    @FieldName = 'Name'

    Select @FieldValue = (Select @FieldName from Contacts where contactid = 32)

    Print @FieldValue

    Always Prinst "Name"


    How do I get the data in Contacts.@FieldName?

    Thanks
    LJ
    Hope is the feeling that the feeling that you have will not last very long.

  4. #4
    Join Date
    Jul 2002
    Location
    Village, MD
    Posts
    621
    Originally posted by LittleJonny
    Dynamic Query? Im not sure I understand.

    @FieldName = 'Name'

    Select @FieldValue = (Select @FieldName from Contacts where contactid = 32)

    Print @FieldValue

    Always Prinst "Name"


    How do I get the data in Contacts.@FieldName?

    Thanks
    LJ
    Dynamic query is something like this:


    create table test(id int, code varchar(10))
    go
    insert test values(1,'A')
    insert test values(2,'B')
    insert test values(3,'C')
    go
    create proc retvalue(@sql varchar(8000),@result varchar(50) output)
    as
    declare @res varchar(50)
    create table #tmp(res varchar(50))
    insert #tmp exec(@sql)
    select @result=res from #tmp
    return
    go
    declare @res varchar(50),@sql varchar(8000),@field varchar(50)
    set @field='code'
    set @sql='select '+@field+' from test'
    exec retvalue @sql,@res output
    select @res

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •