ถ้าแบบนี้จะติด Error Incorrect syntax near '+', Code (MSSQL)
ALTER PROCEDURE [dbo].[UpdateConfig]
@vProfit1 as int,
@vProfit2 as int,
@vProfit3 as int,
@SERVER VARCHAR(MAX)
AS
BEGIN
update @SERVER + dbo.Configuration
set Profit1=@vProfit1,
Profit2=@vProfit2,
Profit3=@vProfit3
END
ถ้าทำรูปแบบนี้จะติด Error Must declare the scalar variable "@vProfit1". Code (MSSQL)
ALTER PROCEDURE [dbo].[UpdateConfig]
@vProfit1 as int,
@vProfit2 as int,
@vProfit3 as int,
@SERVER VARCHAR(MAX)
AS
DECLARE @vQuery nvarchar(MAX)
SET @vQuery = N'
BEGIN
update ' + @SERVER + 'dbo.Configuration
set Profit1=@vProfit1,
Profit2=@vProfit2,
Profit3=@vProfit3
END
'
EXEC (@vQuery)