尝试在C#中保存存储过程时,表附近的语法不正确

时间:2014-05-10 10:23:59

标签: sql sql-server stored-procedures

我为数据库创建了以下3个表:

CREATE TABLE [dbo].[Buyer] (
    [Buyer_Id]   INT           IDENTITY (1, 1) NOT NULL,
    [Last_Name]  NVARCHAR (50) NOT NULL,
    [First_Name] NVARCHAR (50) NOT NULL,
    [Social_No]  NVARCHAR (50) NOT NULL,
    [Phone]      NVARCHAR (50) NOT NULL,
    [User_Id]    INT           NOT NULL,
    PRIMARY KEY CLUSTERED ([Buyer_Id] ASC),
    CONSTRAINT [FK_User_Id] FOREIGN KEY ([User_Id]) REFERENCES [dbo].[User] ([User_Id])
);

CREATE TABLE [dbo].[Type] (
    [Type_Id]   INT        IDENTITY (1, 1) NOT NULL,
    [Type_Name] NCHAR (10) NULL,
    PRIMARY KEY CLUSTERED ([Type_Id] ASC)
);

CREATE TABLE [dbo].[User] (
    [User_Id] INT           IDENTITY (1, 1) NOT NULL,
    [User_Name]    NCHAR (10)    NOT NULL,
    [Pass]    NVARCHAR (50) NOT NULL,
    [Type_Id] INT           NOT NULL,
    PRIMARY KEY CLUSTERED ([User_Id] ASC),
    CONSTRAINT [FK__Type_Id] FOREIGN KEY ([Type_Id]) REFERENCES [dbo].[Type] ([Type_Id])
);

我想创建一个存储过程,目的是在C#中的Addcustumer表单中使用。

Bellow它是我存储过程的代码形式:

CREATE PROCEDURE [dbo].[InsertCustomer]
    @Buyer_Id int output,
    @Last_Name varchar(50),
    @Fist_Name varchar(50),
    @Social_No varchar(50),
    @Phone varchar(50),
    @User_Id int output,
    @User_Name nchar(10),
    @Pass varchar(50),
    @Type_id int output,
    @Type_Name nchar(10)

AS

    insert into Buyer(Buyer_Id,Last_Name,First_Name,Social_No,Phone)
    values (@Last_Name,@Fist_Name,@Social_No)
    set @Buyer_Id=SCOPE_IDENTITY

    insert into User(User_Name,Pass,Type_Id)
    values(@User_Id,@Pass)
    set @Type_id=SCOPE_IDENTITY

    insert into Type(Type_Id,Type_Name)
    values (@Type_Name)
    set @Type_id=SCOPE_IDENTITY

RETURN 0

但是当我尝试运行我的sql存储过程时,我收到以下错误:

  

' User'

附近的语法不正确

请告诉我的代码有什么问题。

3 个答案:

答案 0 :(得分:3)

您的手术存在许多问题。是的,VMai提到的非常重要,你也有以下错误。

CREATE PROCEDURE [dbo].[InsertCustomer]
    @Buyer_Id int output,
    @Last_Name varchar(50),
    @Fist_Name varchar(50),
    @Social_No varchar(50),
    @Phone varchar(50),
    @User_Id int output,
    @User_Name nchar(10),
    @Pass varchar(50),
    @Type_id int output,
    @Type_Name nchar(10)

AS
 BEGIN
   SET NOCOUNT ON;  --<-- to suppress the default sql server message (N) row(s) effected. 

    insert into Buyer(Buyer_Id,Last_Name,First_Name,Social_No,Phone)
    values (@Last_Name,@Fist_Name,@Social_No)
    set @Buyer_Id = SCOPE_IDENTITY();   --<-- Missing Parenthesis 

    insert into [User](User_Name,Pass,Type_Id)
    values(@User_Id,@Pass)
    set @User_Id = SCOPE_IDENTITY();  --<-- Missing Parenthesis  and I think 
                                         --it was suppose to be @User_Id

    insert into [Type](Type_Id,Type_Name)
    values (@Type_Name)
    set @Type_id = SCOPE_IDENTITY();    --<-- Missing Parenthesis 

    RETURN 0

 END

答案 1 :(得分:2)

通常:用户是reserved word in T-SQL。所以你要引用它:

insert into [User] (User_Name,Pass,Type_Id)

答案 2 :(得分:0)

使用此:

CREATE PROCEDURE [dbo].[InsertCustomer]
    @Buyer_Id int output,
    @Last_Name varchar(50),
    @Fist_Name varchar(50),
    @Social_No varchar(50),
    @Phone varchar(50),
    @User_Id int output,
    @User_Name nchar(10),
    @Pass varchar(50),
    @Type_id int output,
    @Type_Name nchar(10)

AS

    insert into Buyer(Buyer_Id,Last_Name,First_Name,Social_No,Phone)
    values (@Last_Name,@Fist_Name,@Social_No)
    set @Buyer_Id=SCOPE_IDENTITY

    insert into User(User_Name,Pass,Type_Id)
    values(@User_Id,@Pass)
    set @Type_id=SCOPE_IDENTITY

    insert into Type(Type_Id,Type_Name)
    values (@Type_Name)
    set @Type_id=SCOPE_IDENTITY

RETURN 0

在上面的代码中:

insert into User(User_Name,Pass,Type_Id)
        values(@User_Id,@Pass)
        set @Type_id=SCOPE_IDENTITY

替换为

insert into [User] (User_Name,Pass,Type_Id)
        values(@User_Id,@Pass)
        set @Type_id=SCOPE_IDENTITY

因为用户是保留字,这就是您收到错误的原因。避免使用这个引用。