我试图在存储过程中连接两个变量,以便在SELECT语句中使用结果字符串。连接按预期工作,但在执行时表未正确填充。这是代码:
Create Procedure CurrencyParameter
@Trade NVARCHAR (5),
@Target NVARCHAR (5)
AS
SELECT
CONCAT(@Trade, '_', SUBSTRING(@Target, CHARINDEX('U', @Target) + 1, LEN(@Target)))
FROM
Trade_Ratios
EXECUTE CurrencyParameter @Trade = 'CU1', @Target ='CU10';
如您所见,该表填充了正确的行数,但只是用连接的字符串替换数据。提前谢谢!
答案 0 :(得分:1)
你需要动态的sql
CREATE PROCEDURE Currencyparameter (@Trade NVARCHAR (5),
@Target NVARCHAR (5))
AS
BEGIN
DECLARE @sql VARCHAR(8000)= ''
SET @sql = Concat('SELECT ', @Trade, '_', Substring(@Target, Charindex( 'U', @Target) + 1, Len(@Target)), ' FROM Trade_Ratios')
EXEC (@sql)
END