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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Dynamic SQL Help

Dynamic SQL Help

Scheduled Pinned Locked Moved Database
databasesharepointcomtoolshelp
5 Posts 3 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.
  • J Offline
    J Offline
    Jammer 0
    wrote on last edited by
    #1

    Hi All, I'm having some troubles working on my first stored proc using dynamic SQL and I've hit a brick wall and can't work out what I'm doing wrong. I have two tables that are always the same structure, however I wish to write a re-useable stored proc to copy records to a new table based on some value comparison. I have this in my stored proc at the moment:

    ALTER PROCEDURE [dbo].[sp_CopyFileScorerChanges]
    -- Add the parameters for the stored procedure here
    @SourceTable NVARCHAR(100),
    @TargetTable NVARCHAR(100)
    AS
    BEGIN
    SET NOCOUNT ON;

    DECLARE @zSQL NVARCHAR(4000)
    
    SET @zSQL = N'INSERT INTO ' + @SourceTable  + '(
    				\[FilePath\],
    				\[CurrentCategory\],
    				\[CurrentSubCategory\],
    				\[CurrentBpm\],
    				\[OldCategory\],
    				\[OldSubCategory\],
    				\[OldBpm\]
    				) 
    				SELECT 
    					@FilePath,
    					@CurrentCategory,
    					@CurrentSubCategory,
    					@CurrentBpm,
    					@OldCategory,
    					@OldSubCategory,
    					@OldBpm
    				FROM  ' + @TargetTable +
    				'WHERE CurrentCategory <> OldCategory'
    				
    			
    EXEC( sp\_ExecuteSQL @zSQL, N'@FilePath NVARCHAR(4000),	
    							 @CurrentCategory NVARCHAR(100),
    							 @CurrentSubCategory NVARCHAR(100),
    							 @CurrentBpm INT,
    							 @OldCategory NVARCHAR(100),
    							 @OldSubCategory NVARCHAR(100),
    							 @OldBpm INT',
    							 @FilePath,	
    							 @CurrentCategory, 
    							 @CurrentSubCategory, 
    							 @CurrentBpm, 
    							 @OldCategory, 
    							 @OldSubCategory, 
    							 @OldBpm
    	)
    

    END

    I'm currently getting a 'Incorrect syntax near 'sp_ExecuteSQL' - Expecting STRING, TEXT_LEX, VARIABLE or GLOBAL_VAR. I just want to call the stored proc with two table names, the source table and the target table. Any help would be really appreciated. Thanks,

    Jammer My Blog | Articles

    M 1 Reply Last reply
    0
    • J Jammer 0

      Hi All, I'm having some troubles working on my first stored proc using dynamic SQL and I've hit a brick wall and can't work out what I'm doing wrong. I have two tables that are always the same structure, however I wish to write a re-useable stored proc to copy records to a new table based on some value comparison. I have this in my stored proc at the moment:

      ALTER PROCEDURE [dbo].[sp_CopyFileScorerChanges]
      -- Add the parameters for the stored procedure here
      @SourceTable NVARCHAR(100),
      @TargetTable NVARCHAR(100)
      AS
      BEGIN
      SET NOCOUNT ON;

      DECLARE @zSQL NVARCHAR(4000)
      
      SET @zSQL = N'INSERT INTO ' + @SourceTable  + '(
      				\[FilePath\],
      				\[CurrentCategory\],
      				\[CurrentSubCategory\],
      				\[CurrentBpm\],
      				\[OldCategory\],
      				\[OldSubCategory\],
      				\[OldBpm\]
      				) 
      				SELECT 
      					@FilePath,
      					@CurrentCategory,
      					@CurrentSubCategory,
      					@CurrentBpm,
      					@OldCategory,
      					@OldSubCategory,
      					@OldBpm
      				FROM  ' + @TargetTable +
      				'WHERE CurrentCategory <> OldCategory'
      				
      			
      EXEC( sp\_ExecuteSQL @zSQL, N'@FilePath NVARCHAR(4000),	
      							 @CurrentCategory NVARCHAR(100),
      							 @CurrentSubCategory NVARCHAR(100),
      							 @CurrentBpm INT,
      							 @OldCategory NVARCHAR(100),
      							 @OldSubCategory NVARCHAR(100),
      							 @OldBpm INT',
      							 @FilePath,	
      							 @CurrentCategory, 
      							 @CurrentSubCategory, 
      							 @CurrentBpm, 
      							 @OldCategory, 
      							 @OldSubCategory, 
      							 @OldBpm
      	)
      

      END

      I'm currently getting a 'Incorrect syntax near 'sp_ExecuteSQL' - Expecting STRING, TEXT_LEX, VARIABLE or GLOBAL_VAR. I just want to call the stored proc with two table names, the source table and the target table. Any help would be really appreciated. Thanks,

      Jammer My Blog | Articles

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      You are trying to use a varable in dynamic sql, you cannot. Everywhere you are using @Var needs to be turned into a literal (like you do for target table). When building dynamic sql I do a print @SQL and try and run the results, this will then give you more detailed results. It will also show that you are trying to insert @FilePath int a varchar without surrounding singles '

      Never underestimate the power of human stupidity RAH

      J J 2 Replies Last reply
      0
      • M Mycroft Holmes

        You are trying to use a varable in dynamic sql, you cannot. Everywhere you are using @Var needs to be turned into a literal (like you do for target table). When building dynamic sql I do a print @SQL and try and run the results, this will then give you more detailed results. It will also show that you are trying to insert @FilePath int a varchar without surrounding singles '

        Never underestimate the power of human stupidity RAH

        J Offline
        J Offline
        Jammer 0
        wrote on last edited by
        #3

        Gotcha. All working now. Thank you!

        Jammer My Blog | Articles

        1 Reply Last reply
        0
        • M Mycroft Holmes

          You are trying to use a varable in dynamic sql, you cannot. Everywhere you are using @Var needs to be turned into a literal (like you do for target table). When building dynamic sql I do a print @SQL and try and run the results, this will then give you more detailed results. It will also show that you are trying to insert @FilePath int a varchar without surrounding singles '

          Never underestimate the power of human stupidity RAH

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          Mycroft Holmes wrote:

          You are trying to use a varable in dynamic sql, you cannot. Everywhere you are using @Var needs to be turned into a literal (like you do for target table).

          Wrong! sp_executesql will allow you to pass parameters into the SQL. He's using string concatenation for the table names because this is one place in SQL you cant use a variable.

          M 1 Reply Last reply
          0
          • J J4amieC

            Mycroft Holmes wrote:

            You are trying to use a varable in dynamic sql, you cannot. Everywhere you are using @Var needs to be turned into a literal (like you do for target table).

            Wrong! sp_executesql will allow you to pass parameters into the SQL. He's using string concatenation for the table names because this is one place in SQL you cant use a variable.

            M Offline
            M Offline
            Mycroft Holmes
            wrote on last edited by
            #5

            You're right, I never use sp_execute, just execute(sql). What was wrong with his proc then?

            Never underestimate the power of human stupidity RAH

            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