Professional Web Applications Themes

How do you set a value to a stored procedure result - Microsoft SQL / MS SQL Server

I am using a lot of variables to find the primary key ( int value )based on an id column and table name. The user determines the unique id and table name. The sp is below and it works fine. create procedure TableLookupProc uniqueid varchar(40), tablename varchar(40) as begin declare colname1 varchar(40), colname2 varchar(40) set colname1 = col_name(object_id(tablename),1) set colname2 = col_name(object_id(tablename),2) declare sqlvars varchar(255) set sqlvars = 'select ' + cast(colname1 as varchar) + ' from ' + cast(tablename as varchar) + ' where ' + cast(colname2 as varchar) + ' like ''' + cast (uniqueid as varchar)+ '''' ...

  1. #1

    Default How do you set a value to a stored procedure result

    I am using a lot of variables to find the primary key
    ( int value )based on an id column and table name. The
    user determines the unique id and table name. The sp is
    below and it works fine.

    create procedure TableLookupProc

    uniqueid varchar(40),
    tablename varchar(40)

    as
    begin


    declare colname1 varchar(40), colname2 varchar(40)

    set colname1 = col_name(object_id(tablename),1)
    set colname2 = col_name(object_id(tablename),2)


    declare sqlvars varchar(255)


    set sqlvars = 'select ' + cast(colname1 as varchar)
    + ' from ' + cast(tablename as varchar) + ' where '
    + cast(colname2 as varchar) + ' like ''' + cast
    (uniqueid as varchar)+ ''''

    exec(sqlvars)



    However I do not know how to set a variable to a
    stored procedure results. If there is a better way
    of doing this feel free to advise, especially
    if this could be done as a function.

    I have tried using OUTPUT and I placed the output varaible
    in the select statement --

    ' select OutputInt = ' + cast(colname1 as varchar) + '
    from ' + cast(tablename as varchar) + ' where ' +
    cast(colname2 as varchar) + ' like ''' + cast
    (uniqueid as varchar)+ ''''

    This will throw an error

    Philip Guest

  2. #2

    Default How do you set a value to a stored procedure result

    Try something like this

    SET OutputInt = (SELECT TOP 1 .... )

    TOP 1 - because if SELECT returned more than one row,
    you'd get a runtime error
     



    Michal Guest

Similar Threads

  1. Is stored procedure safe to query a large result set?
    By howachen@gmail.com in forum MySQL
    Replies: 0
    Last Post: September 1st, 02:15 PM
  2. elp with Multiple Result Sets inside a Stored Procedure
    By ctrl+alt+delete in forum Coldfusion Database Access
    Replies: 4
    Last Post: August 26th, 08:58 PM
  3. Stored Procedure Result Variable
    By Two Tech Guys in forum Coldfusion Database Access
    Replies: 1
    Last Post: February 21st, 05:46 AM
  4. Replies: 28
    Last Post: October 21st, 04:48 PM
  5. Replies: 0
    Last Post: September 11th, 04:35 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139