DECLARE @ProfileType date = (SELECT ProfileType FROM dbo.UserProfiles WHERE UserLogin = @username) if @ProfileType = 1 select isnull(Data, 'NaN') from BIG_TABLE else if @ProfileType = 2 select isnull(Data, 'NaN') from BIG_TABLE where colA = @username else select isnull(Data, 'NaN') from BIG_TABLE where colB = @username End
或者,如果你喜欢使用str exec
DECLARE @ProfileType date = (SELECT ProfileType FROM dbo.UserProfiles WHERE UserLogin = @username) declare @str_sql varchar(1000) set @str_sql = 'select isnull(Data, "NaN") from BIG_TABLE' if @ProfileType = 2 set @str_sql += ' where colA = ' + @username else if @ProfileType = 3 set @str_sql += ' where colB = ' + @username exec(@str_sql)