是否有一种功能可以更快速地检查Microsoft SQL Server Management Studio中给定值是否唯一?不包括以下内容:
DECLARE @username VARCHAR(45)
SET @username = 'foo'
DECLARE @checkTable INT
-- finds how many rows contain 'foo'
SET @checkTable = (SELECT COUNT(1)
FROM table1 AS t
WHERE t.username = @username)
IF @checkTable = 0
-- Unique!
如果你有一张大桌子,这似乎很贵。我需要允许用户更改他/她现有的用户名,同时保持唯一。
由于
答案 0 :(得分:3)
这可以节省几个字符......
DECLARE @username VARCHAR(45)
SET @username = 'foo'
IF NOT EXISTS(SELECT 1 FROM table1 WHERE username = @username)
BEGIN
-- Unique!
END
答案 1 :(得分:1)
IF EXISTS (SELECT 1 FROM table1 WHERE username = @Username)
BEGIN
RETURN 'exists'
END
ELSE
BEGIN
RETURN 'not exists'
END;