2012-01-19 13 views
-1

mysqlベンチでこのクエリを実行するとエラーが発生します。 "Declare @ QuestionnaireID intの近くにSQL構文エラーがあります................"mysqlの構文を宣言します

Begin 
    DECLARE @QuestionnaireID int; 
    INSERT INTO dbo.Questionnaire VALUES('Questionnaire 1'); 
    SET @QuestionnaireID = SCOPE_IDENTITY(); 
    INSERT INTO dbo.QuestionnaireQuestion VALUES(@QuestionnaireID, 1, 'This is the first quetion'); 
    INSERT INTO dbo.QuestionnaireQuestion VALUES(@QuestionnaireID, 2, 'This is the second quetion'); 
    INSERT INTO dbo.QuestionnaireQuestion VALUES(@QuestionnaireID, 3, 'This is the third quetion'); 
    DECLARE @QuestionnaireResponseID int; 
    INSERT INTO dbo.QuestionnaireResponse VALUES(@QuestionnaireID, 1, GETDATE()); 
    SET @QuestionnaireResponseID = SCOPE_IDENTITY(); 
    INSERT INTO dbo.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 1, 'This is answer to first quetion'); 
    INSERT INTO dbo.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 2, 'This is answer to second quetion'); 
    INSERT INTO dbo.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 3, 'This is answer to third quetion'); 

End 

答えて

2

MySQLのユーザー定義変数は宣言する必要はありません。

また、MySQLにはSCOPE_IDENTITY()機能がありません。あなたが探しているのはLAST_INSERT_ID()です。

同様に、MySQLにはGETDATE()という機能がありません。代わりにNOW()を使用してください。

また、INSERT文ごとに挿入する列を明示的に列挙する必要があります。たとえば、アンケート表には自動インクリメントがあるため、挿入する列を列挙する必要があります。そうしないと、挿入が失敗します。

INSERT INTO test.Questionnaire VALUES('Questionnaire 1'); 
SET @QuestionnaireID = LAST_INSERT_ID(); 

INSERT INTO test.QuestionnaireQuestion VALUES(@QuestionnaireID, 1, 'This is the first quetion'); 
INSERT INTO test.QuestionnaireQuestion VALUES(@QuestionnaireID, 2, 'This is the second quetion'); 
INSERT INTO test.QuestionnaireQuestion VALUES(@QuestionnaireID, 3, 'This is the third quetion'); 

INSERT INTO test.QuestionnaireResponse VALUES(@QuestionnaireID, 1, NOW()); 
SET @QuestionnaireResponseID = LAST_INSERT_ID(); 

INSERT INTO test.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 1, 'This is answer to first quetion'); 
INSERT INTO test.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 2, 'This is answer to second quetion'); 
INSERT INTO test.QuestionnaireAnswer VALUES(@QuestionnaireID, 1, 3, 'This is answer to third quetion'); 
:ここ

は、最初の3つの課題を解決するために、あなたのSQLの再書かれています

関連する問題