Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2010
    Posts
    4

    Unanswered: Help in stored procedure

    I have two table
    tblFinance
    tblColumns

    tbl Finance have only one column: Column_Name with values
    C1
    C2
    C3
    C4
    C5
    C6

    tblcolumns have only one column: Column_Name with values
    C1
    C2
    C3
    C6

    I want to the select columns from the tblFinance table
    Like: SELECT C1, C2, C3, C6 from tblFinance but here selection columns should be driven from the column names define tblcolumns
    For this I have written stored procedure this is not working fine, please try to correct the same if possible
    ALTER PROCEDURE ComputeColumns
    AS
    SET NOCOUNT ON
    BEGIN

    DECLARE @name varchar(225)
    DECLARE @stmt varchar(MAX)
    DECLARE @Quary varchar(MAX)

    SET @Quary = 'SELECT '+@stmt + ' from tblfinance'
    DECLARE db_cursor CURSOR FOR
    SELECT Column_Name from tblColumns order by Column_Name


    OPEN db_cursor
    FETCH NEXT FROM db_cursor INTO @name

    WHILE @@FETCH_STATUS = 0
    BEGIN
    SET @stmt += @name + ','


    FETCH NEXT FROM db_cursor INTO @name
    END

    CLOSE db_cursor
    DEALLOCATE db_cursor

    Print @stmt
    Print @Quary

    END

  2. #2
    Join Date
    Jul 2010
    Posts
    4
    Im missing these lines

    SET @stmt = LEFT(@stmt, LEN(@stmt) -1)

    SET @Quary = 'SELECT '+@stmt + ' from finance'

    Its resolved, thanks alot

Posting Permissions

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