2014-10-30 65 views
0

我在寫SQL Server中的井字遊戲2008年 這是我到目前爲止有:語法錯誤。如果X CREATE TABLE語句

print ' 
========================================================== 
Tic Tac Toe - SQL Server 2008 - My Name - 10/30/2014 
========================================================== 
To insert an ''X'', type EXEC pMakeMove row, column, skip. 
Row and column must both be between 1 and 3 or you will 
automatically forfeit your move. skip can be either 1 or 
0, with 1 specifying that you''d like to skip your turn. 

You will always start first, unless you decide to skip 
your first turn. You will always be ''X'' and the PC will 
always be ''O''. 

When you make a move, the PC will automatically make a 
move as well, the game will check for a winner after 
each of these moves. 

After each move, the game board is displayed. 

On a winning move, the game board is displayed, a 
message is displayed, and the board is cleared. 
' 

if not exists (
     select * 
     from INFORMATION_SCHEMA.TABLES 
     where TABLE_SCHEMA = 'dbo' 
     and TABLE_NAME = 'TicTacToe') 
begin 
    create table TicTacToe 
    (
     [row] as int, 
     [1] as bit, 
     [2] as bit, 
     [3] as bit 
    ) 

    insert into TicTacToe (row,[1],[2],[3]) values (1,null,null,null),(2,null,null,null),(3,null,null,null) 
end 
else 
begin 
    update TicTacToe set [1]=null,[2]=null,[3]=null 
end 

這是隨機的錯誤,我'越來越:

Msg 102, Level 15, State 1, Line 31 
Incorrect syntax near ')'. 

我沒有看到任何明顯的原因。

這迫使我添加更多的細節。這就是你需要的所有細節。 StackOverflow有時候很愚蠢。

+1

我不是SQL服務器專家,但是不應該用分號結束語句嗎? – JNevill 2014-10-30 19:55:42

+0

@Yuck他們很好。這只是寫遊戲的便利。使用'[]'應該使這不成問題。 – KthProg 2014-10-30 19:55:55

+0

@JNevill我在每個語句後都添加了分號,我仍然得到了同樣的錯誤。 – KthProg 2014-10-30 19:56:22

回答

3

更改您的發言到此:

create table TicTacToe 
(
    row int, 
    [1] bit, 
    [2] bit, 
    [3] bit 
) 

當定義一個表,你可以不寫as。這是一個相對常見的錯誤,解析器對此沒有多大幫助。

+0

是的,奇怪的是,它沒有抓住這一點。 – KthProg 2014-10-30 19:59:30

+0

我必須等待8分鐘才能接受。 – KthProg 2014-10-30 20:00:18

1

附上row保留在[]中的關鍵字,也缺少;

刪除的也

create table TicTacToe 
(
    [row] int, 
    [1] bit, 
    [2] bit, 
    [3] bit 
); 
+1

真的唯一的問題是在'create'語句中使用'as'。 – Yuck 2014-10-30 19:57:44

+0

我仍然收到相同的錯誤。行不是sql server中的保留字。 – KthProg 2014-10-30 19:57:52

+0

@Yuck你是對的..補充說,作爲答案,我會接受。謝謝。 – KthProg 2014-10-30 19:58:33