4

请问下面的程序语句有什么问题

    DECLARE  @result int 
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    set @result = (select COUNT(*) from populate)

    if (@result > 1)
    Begin
        insert into populate (brch, terminal_id) values(@branch, @atmid)
    end

    SET ANSI_NULLS ON
    GO
     SET QUOTED_IDENTIFIER ON
    GO

    CREATE PROCEDURE insertion @id varchar(50), @brch varchar(50)
    -- Add the parameters for the stored procedure here

   AS
    BEGIN
   DECLARE  @result int 
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    set @result  = (COUNT(*) from populate)

    if (@result > 1)
    Begin
        insert into populate (brch, terminal_id) values(@id, @brch)
    end
    END
    GO
4

3 回答 3

1

似乎您首先发布了一段给出错误的代码,Msg 137, Level 15, State 2, Line 11 Must declare the scalar variable "@branch".然后添加了一个给出错误的完整过程,从而使事情变得混乱Msg 156, Level 15, State 1, Procedure insertion, Line 13 Incorrect syntax near the keyword 'from'.

请确保您发布您正在使用的真实代码以及完整的错误消息,否则人们无法帮助您。

无论如何,我忽略了代码片段,只查看了过程,正如 ABFORCE 所说,问题在于您填充的位置,@result因为您的语法错误。此过程代码在 SQL Server 2008 中解析没有错误:

   CREATE PROCEDURE insertion @id varchar(50), @brch varchar(50)
    -- Add the parameters for the stored procedure here

   AS
    BEGIN
   DECLARE  @result int 
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    select @result  = COUNT(*) from populate

    if (@result > 1)
    Begin
        insert into populate (brch, terminal_id) values(@id, @brch)
    end
    END
    GO

您可能需要查看为变量和关键字赋值的文档。SET

于 2012-12-14T12:27:12.033 回答
0

正如错误消息中所说的那样,问题是在@branch使用@atmid它们的值之前没有提到任何地方。

你已经声明@resultset它的价值,所以你需要做同样的事情,@branch而且@atmid系统无法为你预测价值。

于 2012-12-14T09:24:31.893 回答
0

尝试这个

select @result =COUNT(*) from populate
于 2013-07-11T06:42:08.347 回答