dynamic sql

  • Hi ,

    @col_name= column name coming rom cursor

    @ColValue= will store column value

    select @ColValue=@col_name

    from dbo.tblBlo

    WHERE Customer_no = @CustomerNumber

    'AND name = @UserName);

    @col_name, will be dynamically pass from loop and will assign column value to @ColValue.

    But not working 🙁

    How can I make such a dynamic query?

  • thbaig1 (5/19/2011)


    Hi ,

    @col_name= column name coming rom cursor

    @ColValue= will store column value

    select @ColValue=@col_name

    from dbo.tblBlo

    WHERE Customer_no = @CustomerNumber

    'AND name = @UserName);

    @col_name, will be dynamically pass from loop and will assign column value to @ColValue.

    But not working 🙁

    How can I make such a dynamic query?

    You are not assigning a Value to the Variable to @col_Name

    select @ColValue=@col_name

    select @ColValue=col_name -- Should be a Column in the Table

    You have not Devalred or assigned values to @CustomerNumber and @UserName

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • Welsh Corgi (5/19/2011)


    thbaig1 (5/19/2011)


    Hi ,

    @col_name= column name coming rom cursor

    @ColValue= will store column value

    select @ColValue=@col_name

    from dbo.tblBlo

    WHERE Customer_no = @CustomerNumber

    'AND name = @UserName);

    @col_name, will be dynamically pass from loop and will assign column value to @ColValue.

    But not working 🙁

    How can I make such a dynamic query?

    You are not assigning a Value to the Variable to @col_Name

    select @ColValue=@col_name

    select @ColValue=col_name -- Should be a Column in the Table

    You have not Devalred or assigned values to @CustomerNumber and @UserName

    as i try to explain that this code is part of a stored proc.

    @CustomerNumber and @UserName are declared and will have value on execution.

    @col_name is a result of a query that is giving me column name

    @ColValue is a variable to store column (@col_name) value .

    so I want that stored proc translate @col_name as column name and assign value to @ColValue.

  • thbaig1 (5/19/2011)


    Welsh Corgi (5/19/2011)


    thbaig1 (5/19/2011)


    Hi ,

    @col_name= column name coming rom cursor

    @ColValue= will store column value

    select @ColValue=@col_name

    from dbo.tblBlo

    WHERE Customer_no = @CustomerNumber

    'AND name = @UserName);

    @col_name, will be dynamically pass from loop and will assign column value to @ColValue.

    But not working 🙁

    How can I make such a dynamic query?

    You are not assigning a Value to the Variable to @col_Name

    select @ColValue=@col_name

    select @ColValue=col_name -- Should be a Column in the Table

    You have not Devalred or assigned values to @CustomerNumber and @UserName

    as i try to explain that this code is part of a stored proc.

    @CustomerNumber and @UserName are declared and will have value on execution.

    @col_name is a result of a query that is giving me column name

    @ColValue is a variable to store column (@col_name) value .

    so I want that stored proc translate @col_name as column name and assign value to @ColValue.

    I'm sorry but your logic is Severely flawed.:w00t:

    For better, quicker answers on T-SQL questions, click on the following...
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    For better answers on performance questions, click on the following...
    http://www.sqlservercentral.com/articles/SQLServerCentral/66909/

  • I'm sorry but your logic is Severely flawed.:w00t:

    I figured that when I saw

    @col_name= column name coming rom cursor

    at the very top.

    If you really want some help you need to help us help you. Please see the link in my signature about how post questions. If you provide these details we can not only help you figure out a solution we can almost certainly get rid of that nasty cursor. 😉

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (5/19/2011)


    I'm sorry but your logic is Severely flawed.:w00t:

    I figured that when I saw

    @col_name= column name coming rom cursor

    at the very top.

    If you really want some help you need to help us help you. Please see the link in my signature about how post questions. If you provide these details we can not only help you figure out a solution we can almost certainly get rid of that nasty cursor. 😉

    well you can say 🙂

    again this is really a trouble to handle bad design and to do all with logic 🙁

    as I told I am getting column name from sys and using those name in dynamic query.

    Suppose fname is column name. and cursor is placing this column name in @col_name.

    now @col_name=fname

    so I want

    select @col_name from table may act as select fname from table.

    2nd thing

    select @colValue= @col_name from table may act as select @colValue=fname from table;

  • Like I said, if you want us to help you, you have to provide us some information. There are very few cases where set based logic can't replace a cursor. Cursors are horribly slow due to the nature of processing data one row at a time. Then you add dynamic sql inside a cursor it is a recipe for disaster.

    If you are dead set against getting some help to make your process faster, better, stronger then take this snippet ...

    declare @sql varchar(max)

    set @sql = 'select ' + @col_name + ' from table'

    select @sql

    exec(@sql)

    If you want some help to make your process better please provide ddl and sample data and what you are trying to accomplish in a format that makes it easy for us to help (create table, inserts, details about the process). If this is good enough for you then I hope I was able to help.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply