Sql server:Msg 102,Level 15,State 1执行存储过程时

时间:2015-01-05 16:01:53

标签: sql-server-2008

使用动态sql执行存储过程时出错 这是我的存储过程:

CREATE PROCEDURE [dbo].[SP_InsertMagasinsInTable]
    (
    @Code varchar(15),
    @Surface int,
    @Lieu varchar(80),
    @Adresse varchar(150),
    @CodePostal varchar(10),
    @IDTCommune int,    
    @IDTPays int,
    @Ville varchar(80),
    @IdEnseigne int,
    @LibMagasin varchar(50),
    @TelStandard varchar(30),
    @IdClient int,
    @DateCreation datetime ,
    @AcrCreateur char(6)
    )
AS
BEGIN
    DECLARE @CMD as varchar(1000)

SET @CMD='INSERT INTO MAG_Magasin
           (MAG_Magasin.NumMag
           , MAG_Magasin.Surface
           , MAG_Magasin.Lieu
           , MAG_Magasin.Adresse
           , MAG_Magasin.CodePostal
           , MAG_Magasin.IDTCommune
           , MAG_Magasin.IDTPays
           , MAG_Magasin.Ville
           , MAG_Magasin.IdEnseigne
           , MAG_Magasin.LibMagasin
           , MAG_Magasin.TelStandard
           , MAG_Magasin.IdClient
           , MAG_Magasin.DateCreation
           , MAG_Magasin.AcrCreateur)
       VALUES ('''+
            @Code + ''',' +
            CAST(@Surface AS VARCHAR) + ','''+
            @Lieu + ''','''+
            @Adresse + ''','''+
            @CodePostal + ''','''+              
            CAST(@IDTCommune AS VARCHAR) + ','+
            CAST(@IDTPays AS VARCHAR) + ','+
            @Ville + ''','+
            CAST(@IdEnseigne AS VARCHAR) + ','''+
            @LibMagasin + ''','''+
            @TelStandard + ''','+
            CAST(@IdClient AS VARCHAR) + ','+
            CAST(@DateCreation AS VARCHAR) + ''','''+
            @AcrCreateur +
            ')'                 

EXEC(@CMD)

END

EXEC SP_InsertMagasinsInTable   
    @Code = '234',
    @Surface = 245,
    @Lieu = 'Lieu',
    @Adresse ='Adresse',
    @CodePostal =  '78120',
    @IDTCommune = 45,   
    @IDTPays = 33,
    @Ville = 'Ville',
    @IdEnseigne = 42,
    @LibMagasin = 'LibMagasin',
    @TelStandard = '0123456789',
    @IdClient = 452,
    @DateCreation = '15:08:03',
    @AcrCreateur ='acr'

这是我的错误:

  

Msg 102,Level 15,State 1,Line 16   “1”附近的语法不正确。

1 个答案:

答案 0 :(得分:0)

你有一些缺失的引号,这就是你的@cmd返回的内容:

INSERT INTO MAG_Magasin
           (MAG_Magasin.NumMag
           , MAG_Magasin.Surface
           , MAG_Magasin.Lieu
           , MAG_Magasin.Adresse
           , MAG_Magasin.CodePostal
           , MAG_Magasin.IDTCommune
           , MAG_Magasin.IDTPays
           , MAG_Magasin.Ville
           , MAG_Magasin.IdEnseigne
           , MAG_Magasin.LibMagasin
           , MAG_Magasin.TelStandard
           , MAG_Magasin.IdClient
           , MAG_Magasin.DateCreation
           , MAG_Magasin.AcrCreateur)
       VALUES ('234',245,'Lieu','Adresse','78120','45,33,Ville',42,'LibMagasin','0123456789',452,Jan  1 1900  3:08PM','acr   )

似乎@CMD应该是这样的:

SET @CMD='INSERT INTO MAG_Magasin
           (MAG_Magasin.NumMag
           , MAG_Magasin.Surface
           , MAG_Magasin.Lieu
           , MAG_Magasin.Adresse
           , MAG_Magasin.CodePostal
           , MAG_Magasin.IDTCommune
           , MAG_Magasin.IDTPays
           , MAG_Magasin.Ville
           , MAG_Magasin.IdEnseigne
           , MAG_Magasin.LibMagasin
           , MAG_Magasin.TelStandard
           , MAG_Magasin.IdClient
           , MAG_Magasin.DateCreation
           , MAG_Magasin.AcrCreateur)
       VALUES ('''+
            @Code + ''',' +
            CAST(@Surface AS VARCHAR) + ','''+
            @Lieu + ''','''+
            @Adresse + ''','''+
            @CodePostal + ''','+              
            CAST(@IDTCommune AS VARCHAR) + ','+
            CAST(@IDTPays AS VARCHAR) + ','''+ 
            @Ville + ''','+
            CAST(@IdEnseigne AS VARCHAR) + ','''+
            @LibMagasin + ''','''+
            @TelStandard + ''','+
            CAST(@IdClient AS VARCHAR) + ','''+
            CAST(@DateCreation AS VARCHAR) + ''','''+
            @AcrCreateur +
            ''')'