2017-08-14 49 views
0

//我的代碼應該是這樣的打印錯誤表中有一個名稱已經

CREATE PROCEDURE REGISTRATION 
@USERNAME VARCHAR(20) 
AS 
BEGIN 
BEGIN IF 
SELECT USERNAME FROM USERS WHERE USERNAME = @USERNAME 
PRINT 'USER ALREADY EXITS' 
ELSE 
IF @USERNAME = 'NULL' 
PRINT 'Fill username' 
end 
end 
+0

'IF EXISTS(SELECT用戶名來自用戶的其中username = ...'或'@@ ROWCOUNT'可能是你想要什麼,但是這不應該在完成數據庫,通過檢查查詢結果來更好地檢查用戶是否存在或爲空。 – Metaphox

+0

如果可能,您可以鍵入該查詢 – Sasank

回答

0

試試這個:

CREATE PROCEDURE REGISTRATION 
@USERNAME VARCHAR(20) 
AS 
BEGIN 
declare @count as int 
select @count = COUNT(*) from USER where USERNAME [email protected] 
if(@count > 0) 
print 'USER ALREADY EXITS' -- Record Exists 
else 
print 'Fill username' -- NULL 
end 
0

使用存在。此外,格式化被忽視,是重要的

CREATE PROCEDURE REGISTRATION 
@USERNAME VARCHAR(20)='' 
AS 
BEGIN 
    IF exists(SELECT 1 FROM USERS WHERE USERNAME = @USERNAME) 
     begin 
      PRINT 'USER ALREADY EXITS' 
     end 
    ELSE 
     begin 
      PRINT 'Fill username' 
     end 
end 
相關問題