2013-03-26 51 views
-1

,而試圖在SQL Server中執行我不斷收到一個錯誤:SQL語法不正確,當試圖CREATE PROCEDURE

消息156,級別15,狀態1,過程sql_chp4a_premiere_assign,5號線
附近有語法錯誤的關鍵字'SELECT'。

我假設它指的是第一個SELECT。我對SQL還不是很熟悉(以及由於可怕的風暴和Spring Break而暫時沒有上課),所以這可能是我忽略的一些愚蠢的東西。

USE [school username that gives out my name) 

CREATE PROCEDURE sql_chp4a_premiere_assign 

SELECT PART_NUM,DESCRIPTION,PRICE 
FROM PART; 

SELECT * 
FROM ORDERS 

SELECT CUSTOMER_NAME 
FROM customer 
WHERE CREDIT_LIMIT>=10000; 

SELECT ORDER_NUM 
FROM ORDERS 
WHERE CUSTOMER_NUM = '608' 
AND ORDER_DATE = '2010-10-23'; 

SELECT CUSTOMER_NUM,CUSTOMER_NAME 
FROM CUSTOMER 
WHERE REP_NUM=35 OR REP_NUM=65; 

SELECT PART_NUM,DESCRIPTION 
FROM PART WHERE CLASS!='AP'; 

SELECT PART_NUM,DESCRIPTION,ON_HAND 
FROM PART 
WHERE ON_HAND BETWEEN 10 AND 25; 

SELECT PART_NUM,DESCRIPTION,ON_HAND 
FROM PART 
WHERE ON_HAND>=10 AND ON_HAND<=25; 

SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
AS "ON_HAND_VALUE" 
FROM PART WHERE CLASS='SG'; 

SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
AS "ON_HAND_VALUE" 
FROM PART 
WHERE (ON_HAND * PRICE)>7500; 

SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
AS "ON_HAND_VALUE" 
FROM PART WHERE (ON_HAND * PRICE)>7500; 

謝謝,我真的很感謝任何幫助。

+5

這個錯誤是因爲你錯過了'AS' - [文件](http://msdn.microsoft.com/en-us/library/ms345415.aspx)。其次,這個片子選擇了很多,但卻無所作爲。 – 2013-03-26 02:38:43

回答

1

第一:

你錯了。在這一部分:

USE [school username that gives out my name) 

變化)]

USE [school username that gives out my name] 

二:

你錯過ASBEGIN和存儲過程聲明END關鍵字。

三:

你創建了兩個存儲過程。分離與GO

每個存儲過程聲明這將工作:

USE [school username that gives out my name] 

go 

CREATE PROCEDURE Sql_chp4a_premiere_assign 
AS 
    BEGIN 
     SELECT part_num, 
      description, 
      price 
     FROM part; 

     SELECT * 
     FROM orders 

     SELECT customer_name 
     FROM customer 
     WHERE credit_limit >= 10000; 

     SELECT order_num 
     FROM orders 
     WHERE customer_num = '608' 
      AND order_date = '2010-10-23'; 

     SELECT customer_num, 
      customer_name 
     FROM customer 
     WHERE rep_num = 35 
       OR rep_num = 65; 

     SELECT part_num, 
      description 
     FROM part 
     WHERE class != 'AP'; 

     SELECT part_num, 
      description, 
      on_hand 
     FROM part 
     WHERE on_hand BETWEEN 10 AND 25; 

     SELECT part_num, 
      description, 
      on_hand 
     FROM part 
     WHERE on_hand >= 10 
      AND on_hand <= 25; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE class = 'SG'; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE (on_hand * price) > 7500; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE (on_hand * price) > 7500; 
    END; 

go 

CREATE PROCEDURE Sql_chp4a_premiere_assign 
AS 
    BEGIN 
     SELECT part_num, 
      description, 
      price 
     FROM part; 

     SELECT * 
     FROM orders 

     SELECT customer_name 
     FROM customer 
     WHERE credit_limit >= 10000; 

     SELECT order_num 
     FROM orders 
     WHERE customer_num = '608' 
      AND order_date = '2010-10-23'; 

     SELECT customer_num, 
      customer_name 
     FROM customer 
     WHERE rep_num = 35 
       OR rep_num = 65; 

     SELECT part_num, 
      description 
     FROM part 
     WHERE class != 'AP'; 

     SELECT part_num, 
      description, 
      on_hand 
     FROM part 
     WHERE on_hand BETWEEN 10 AND 25; 

     SELECT part_num, 
      description, 
      on_hand 
     FROM part 
     WHERE on_hand >= 10 
      AND on_hand <= 25; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE class = 'SG'; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE (on_hand * price) > 7500; 

     SELECT part_num, 
      description, 
      (on_hand * price) AS "ON_HAND_VALUE" 
     FROM part 
     WHERE (on_hand * price) > 7500; 
    END 

存儲過程的語法見here

無論如何,您可以使用this來格式化您的SQL。

+0

這給我「」消息111,級別15,狀態1,過程sql_chp4a_premiere_assign,第8行'CREATE/ALTER PROCEDURE'必須是查詢批處理中的第一條語句。「」 – user2109696 2013-03-26 02:56:04

+0

@ user2109696:單獨'使用'與創建存儲過程'使用'GO' ..我編輯了我的答案 – 2013-03-26 03:07:17

+0

'AS'是必需的。'BEGIN'和'END'不是(雖然它們經常被使用) – 2013-03-26 07:13:20

2

更改此:

CREATE PROCEDURE sql_chp4a_premiere_assign 

這樣:

CREATE PROCEDURE sql_chp4a_premiere_assign AS 
+0

Michael將錯誤更改爲「Msg 111,Level 15,State 1,Procedure sql_chp4a_premiere_assign,Line 8 'CREATE/ALTER PROCEDURE'必須是查詢批處理中的第一條語句。」 – user2109696 2013-03-26 02:51:07

+0

@user,在USE語句後放一個GO。 – 2013-03-26 02:53:03

+0

請嘗試我的文章 – 2013-03-26 05:50:28

0

試試這個

CREATE PROCEDURE sql_chp4a_premiere_assign 
AS 
BEGIN 
    SELECT PART_NUM,DESCRIPTION,PRICE 
    FROM PART; 

    SELECT * 
    FROM ORDERS 

    SELECT CUSTOMER_NAME 
    FROM customer 
    WHERE CREDIT_LIMIT>=10000; 

    SELECT ORDER_NUM 
    FROM ORDERS 
    WHERE CUSTOMER_NUM = '608' 
    AND ORDER_DATE = '2010-10-23'; 

    SELECT CUSTOMER_NUM,CUSTOMER_NAME 
    FROM CUSTOMER 
    WHERE REP_NUM=35 OR REP_NUM=65; 

    SELECT PART_NUM,DESCRIPTION 
    FROM PART WHERE CLASS!='AP'; 

    SELECT PART_NUM,DESCRIPTION,ON_HAND 
    FROM PART 
    WHERE ON_HAND BETWEEN 10 AND 25; 

    SELECT PART_NUM,DESCRIPTION,ON_HAND 
    FROM PART 
    WHERE ON_HAND>=10 AND ON_HAND<=25; 

    SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
    AS "ON_HAND_VALUE" 
    FROM PART WHERE CLASS='SG'; 

    SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
    AS "ON_HAND_VALUE" 
    FROM PART 
    WHERE (ON_HAND * PRICE)>7500; 

    SELECT PART_NUM,DESCRIPTION,(ON_HAND * PRICE) 
    AS "ON_HAND_VALUE" 
    FROM PART WHERE (ON_HAND * PRICE)>7500; 
END