10

我创建了一个存储过程。首先我检查一个表是否在我的数据库中,然后我删除它并创建新表,其次我在 SP 中创建一个存储过程,我在表中插入值。我的问题是当我在存储过程中添加存储过程部分时出现错误incorrect syntax near begin expecting EXTERNAL。以下是我的商店程序,你能帮我解决我做错的地方吗?

ALTER PROCEDURE myProcedure
AS 
BEGIN

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ATSROUTES]') AND type in (N'U'))
DROP TABLE [dbo].[ATSROUTES]


    CREATE TABLE [ATSROUTES](
    [zip] [varchar](255) NULL,
    [route] [varchar](255) NULL,
    [drivernum] [varchar](255) NULL,
    [altserviceid] [varchar](255) NULL,
    [localorldrvnum] [varchar](255) NULL,
    [pickupzone] [varchar](255) NULL,
    [distcenter] [varchar](255) NULL,
    [altdispid] [varchar](255) NULL,
    [id] [int] NULL);

    BULK INSERT ATSROUTES FROM  'C:\Routes\my1.csv' WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')


IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[updateroute_sp]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].updateroute_sp

CREATE procedure updateroute_sp 
AS
BEGIN

    DECLARE @route varchar(255)
    DECLARE @zip varchar(255)
    DECLARE @routeid varchar(255)
    DECLARE @distcenter varchar(255)
    DECLARE @altdispid varchar(255)



    DECLARE @ERR_NOTFOUND varchar(2000)

    DECLARE db_Cursor CURSOR FOR SELECT zip, [route] from ATSROUTES 

OPEN db_Cursor 

    FETCH NEXT FROM db_Cursor INTO @zip,@route
    WHILE @@FETCH_STATUS = 0

     BEGIN
        IF (@route is not null and @route <> '') 
        BEGIN
        IF NOT EXISTS (SELECT * FROM routenames WHERE routename = @ROUTE)
         BEGIN
            EXEC GETNEWRECID2 'ROUTENAMES','ROUTEID','ROUTENAMES',@ROUTEID
            insert into routenames (routeid,routename) values (@routeid,@ROUTE);
         END

    END

 FETCH NEXT FROM db_Cursor INTO @zip,@route

   END
        CLOSE db_Cursor
        DEALLOCATE db_Cursor
   END


   EXEC updateroute_sp 

   DROP PROCEDURE updateroute_sp

   UPDATE ATSROUTES set id = (select routeid from routenames where routename = [route]);
   UPDATE ATSROUTES set drivernum =LTRIM(RTRIM(drivernum));

   END

谢谢 。

4

1 回答 1

10

关键字“AS”在由“GO”或“;”标记的批次结束前出现两次。

将 SP 的创建封装在一个字符串中(动态 SQL),然后如果要使用另一个 SP 创建一个 SP,则使用 E​​XEC(@SQL) 调用该字符串的执行。

于 2013-11-18T18:50:22.237 回答