我有以下创建视图的存储过程:
ALTER PROC Proc_Guards_By_Client
(
@client_number INT,
@client_name NVARCHAR(16)
)
AS
BEGIN
IF EXISTS(select * FROM sys.views where name = 'vwGuardsByClients')
BEGIN
EXEC ('CREATE VIEW vwGuardsByClients
AS
SELECT TOP 1000
cgt.[guard_id],
sg.first_name,
sg.last_name,
sg.ammunition_quantity
FROM [sws4].[dbo].[client_guard_tracking] cgt
INNER JOIN CLIENTS c
ON c.client_number = cgt.client_number
INNER JOIN security_guard sg
ON sg.guard_id = cgt.guard_id
WHERE cgt.client_number = @client_number
OR c.client_name = @client_name
')
END
ELSE
BEGIN
EXEC ('UPDATE VIEW vwGuardsByClients
SELECT TOP 1000
cgt.[guard_id],
sg.first_name,
sg.last_name,
sg.ammunition_quantity
FROM [sws4].[dbo].[client_guard_tracking] cgt
INNER JOIN CLIENTS c
ON c.client_number = cgt.client_number
INNER JOIN security_guard sg
ON sg.guard_id = cgt.guard_id
WHERE cgt.client_number = @client_number
OR c.client_name = @client_name
')
END
IF @@ROWCOUNT = 0
PRINT 'Warning: No rows were updated'
END
但是当我执行它时,我得到:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'VIEW'.
Msg 137, Level 15, State 2, Line 14
Must declare the scalar variable "@client_number".