Dynamic SQL INSERT Newbie Question

  • EXEC sp_executesql ...

    Syntax is in BOL.

  • Why can't you do it this way? I am not sure why you are using variables in the select statement:

    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 = 'INSERT INTO ' + @TargetTable + '(

    [FilePath],

    [CurrentCategory],

    [CurrentSubCategory],

    [CurrentBpm],

    [OldCategory],

    [OldSubCategory],

    [OldBpm]

    )

    SELECT

    [FilePath],

    [CurrentCategory],

    [CurrentSubCategory],

    [CurrentBpm],

    [OldCategory],

    [OldSubCategory],

    [OldBpm]

    FROM ' + @SourceTable +

    'WHERE CurrentCategory OldCategory'

    EXEC (@zSQL)

    END

Viewing 2 posts - 1 through 3 (of 3 total)

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