【发布时间】:2013-12-03 03:08:17
【问题描述】:
我对 SQL Server 很陌生,所以希望这是有道理的 :)
我正在尝试声明要在 INNER JOIN 中使用的变量。
如果您看一下我的代码,您就会明白我正在尝试做什么,而无需我过多介绍。如果您需要更多信息,请告诉我。这种语法可能吗?
编辑:请参阅下面的新尝试
--State - If suburb/postcode, could use postcode lookup
Declare @Missing as nvarchar(255),
@MissingUpdate as nvarchar(255),
@MatchA as nvarchar(255),
@MatchB as nvarchar(255),
@Reason as nvarchar(255);
Set @Missing = '[StateEXPORT]'; -- field to update
Set @MissingUpdate = '[State]'; -- field in postcode lookup to pull in
Set @MatchA = '[PostcodeEXPORT]'; -- field in master field to match with
Set @MatchB = '[Pcode]'; -- field in postcode lookup to match with
Set @Reason = 'Contactable - Needs verificiation - @MissingUpdate taken from Lookup'; -- reason here
update [BT].[dbo].[test]
set @Missing = b.@MissingUpdate,
FinalPot = @Reason
FROM [BT].[dbo].[test] a
INNER JOIN [BT].[dbo].[Postcode Lookup] b
ON a.@MatchA = b.@MatchB
where (@Missing is null or @Missing = '0') and [AddressSource] != ('Uncontactable')
GO
编辑:第二次尝试:
set @sql = 'update [BT].[dbo].[test] set ' + quotename(@Missing) + '= b.' + quotename(@MissingUpdate) + ', FinalPot = ' + @Reason + 'FROM [BT].[dbo].[test] a INNER JOIN [BT].[dbo].[Postcode Lookup] b ON a.' + quotename(@MatchA) + ' = b.' + quotename(@MatchB) + 'where (' + quotename(@Missing) + 'is null or' + quotename(@Missing) + ' = 0 and [AddressSource] != "(Uncontactable)"'
exec (@sql)
感谢您的帮助, 卢卡斯
【问题讨论】:
标签: sql variables join sql-server-2012 declare