Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Question for an expert Transact SQL GURU!

Question for an expert Transact SQL GURU!

Scheduled Pinned Locked Moved Database
databasequestionsharepointsql-serversysadmin
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LizardWiz
    wrote on last edited by
    #1

    Has anyone ever figured out a way to use "sp_executesql" on a stored procedure that returns an output parameter. It seems as though when you execute "sp_executesql" SQL server compiles the syntax using a different profile. I can't seem to access any variables declared in the SQL string (even using an optional declaration parameter) nor can I access any variables declared within a stored procedure from within the "sp_executesql" string. It just doesn't seem possible. Anyone got any creative ideas? example: sp_executesql "exec sp_name @varname output" or: sp_executesql "exec sp_name @varname output", @varname int ~LizardWiz()

    M M O 3 Replies Last reply
    0
    • L LizardWiz

      Has anyone ever figured out a way to use "sp_executesql" on a stored procedure that returns an output parameter. It seems as though when you execute "sp_executesql" SQL server compiles the syntax using a different profile. I can't seem to access any variables declared in the SQL string (even using an optional declaration parameter) nor can I access any variables declared within a stored procedure from within the "sp_executesql" string. It just doesn't seem possible. Anyone got any creative ideas? example: sp_executesql "exec sp_name @varname output" or: sp_executesql "exec sp_name @varname output", @varname int ~LizardWiz()

      M Offline
      M Offline
      mozka
      wrote on last edited by
      #2

      acording to the SQLServer BOL, that can't be done, variables used within the query being executed by sp_executesql are local to the query therefore they can't be accessed by the query that executed it in the first place i hope i made myself clear with that :|

      1 Reply Last reply
      0
      • L LizardWiz

        Has anyone ever figured out a way to use "sp_executesql" on a stored procedure that returns an output parameter. It seems as though when you execute "sp_executesql" SQL server compiles the syntax using a different profile. I can't seem to access any variables declared in the SQL string (even using an optional declaration parameter) nor can I access any variables declared within a stored procedure from within the "sp_executesql" string. It just doesn't seem possible. Anyone got any creative ideas? example: sp_executesql "exec sp_name @varname output" or: sp_executesql "exec sp_name @varname output", @varname int ~LizardWiz()

        M Offline
        M Offline
        michanne
        wrote on last edited by
        #3

        Actually you can do it. :cool: What you are missing is the that it needs to be an nvarchar. this is from http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262499 \CREATE PROCEDURE Myproc @parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT AS SELECT @parm1OUT='parm 1' + @parm SELECT @parm2OUT='parm 2' + @parm GO DECLARE @SQLString NVARCHAR(500) DECLARE @ParmDefinition NVARCHAR(500) DECLARE @parmIN VARCHAR(10) DECLARE @parmRET1 VARCHAR(30) DECLARE @parmRET2 VARCHAR(30) SET @parmIN=' returned' SET @SQLString=N'EXEC Myproc @parm, @parm1OUT OUTPUT, @parm2OUT OUTPUT' SET @ParmDefinition=N'@parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT' EXECUTE sp_executesql @SQLString, @ParmDefinition, @parm=@parmIN, @parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT SELECT @parmRET1 AS "parameter 1", @parmRET2 AS "parameter 2" go drop procedure Myproc Barbara, MCP

        L M 2 Replies Last reply
        0
        • M michanne

          Actually you can do it. :cool: What you are missing is the that it needs to be an nvarchar. this is from http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262499 \CREATE PROCEDURE Myproc @parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT AS SELECT @parm1OUT='parm 1' + @parm SELECT @parm2OUT='parm 2' + @parm GO DECLARE @SQLString NVARCHAR(500) DECLARE @ParmDefinition NVARCHAR(500) DECLARE @parmIN VARCHAR(10) DECLARE @parmRET1 VARCHAR(30) DECLARE @parmRET2 VARCHAR(30) SET @parmIN=' returned' SET @SQLString=N'EXEC Myproc @parm, @parm1OUT OUTPUT, @parm2OUT OUTPUT' SET @ParmDefinition=N'@parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT' EXECUTE sp_executesql @SQLString, @ParmDefinition, @parm=@parmIN, @parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT SELECT @parmRET1 AS "parameter 1", @parmRET2 AS "parameter 2" go drop procedure Myproc Barbara, MCP

          L Offline
          L Offline
          LizardWiz
          wrote on last edited by
          #4

          WOW! Works great! Thanks alot....I thought I was a T-SQL GURU but I acquiesce the title to you. ~LizardWiz()

          1 Reply Last reply
          0
          • M michanne

            Actually you can do it. :cool: What you are missing is the that it needs to be an nvarchar. this is from http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262499 \CREATE PROCEDURE Myproc @parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT AS SELECT @parm1OUT='parm 1' + @parm SELECT @parm2OUT='parm 2' + @parm GO DECLARE @SQLString NVARCHAR(500) DECLARE @ParmDefinition NVARCHAR(500) DECLARE @parmIN VARCHAR(10) DECLARE @parmRET1 VARCHAR(30) DECLARE @parmRET2 VARCHAR(30) SET @parmIN=' returned' SET @SQLString=N'EXEC Myproc @parm, @parm1OUT OUTPUT, @parm2OUT OUTPUT' SET @ParmDefinition=N'@parm varchar(10), @parm1OUT varchar(30) OUTPUT, @parm2OUT varchar(30) OUTPUT' EXECUTE sp_executesql @SQLString, @ParmDefinition, @parm=@parmIN, @parm1OUT=@parmRET1 OUTPUT,@parm2OUT=@parmRET2 OUTPUT SELECT @parmRET1 AS "parameter 1", @parmRET2 AS "parameter 2" go drop procedure Myproc Barbara, MCP

            M Offline
            M Offline
            mozka
            wrote on last edited by
            #5

            thanks for pointing that out, i made some test myself and by lookign at your code i see that i was missing the OUTPUT statement in the parameters that were returning a value

            1 Reply Last reply
            0
            • L LizardWiz

              Has anyone ever figured out a way to use "sp_executesql" on a stored procedure that returns an output parameter. It seems as though when you execute "sp_executesql" SQL server compiles the syntax using a different profile. I can't seem to access any variables declared in the SQL string (even using an optional declaration parameter) nor can I access any variables declared within a stored procedure from within the "sp_executesql" string. It just doesn't seem possible. Anyone got any creative ideas? example: sp_executesql "exec sp_name @varname output" or: sp_executesql "exec sp_name @varname output", @varname int ~LizardWiz()

              O Offline
              O Offline
              OMalleyW
              wrote on last edited by
              #6

              kinda nasty... but check this out http://www.sqlmag.com/Forums/messageview.cfm?catid=22&threadid=722

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups