大家好,我正在选择相同列的存储过程,我的查询如下
CREATE PROCEDURE GET_DATA
@in_filter_column1 NVARCHAR(6),
@in_filter_column2 NVARCHAR(50),
@in_flag BIT = 0
AS
BEGIN
IF @in_flag = 0
SELECT t1.COLUMN1, t1.COLUM2, t2.COLUMN3
FROM table1 t1
INNER JOIN table2 t2
ON t1.column1 = t2.column1
WHERE t1.column1 = @in_filter_column1
ELSE
SELECT t1.COLUMN1, t1.COLUM2, t2.COLUMN3
FROM table1 t1
INNER JOIN table2 t2
ON t1.column1 = t2.column1
INNER JOIN table3 t3
ON t3.column1 = t2.column1
WHERE t3.column1 = @in_filter_column2
END
所以这里不是写相同的选择条件我想简单地使用开关盒或其他替代方式。是否可能
我知道的一种方式就是这样我正在尝试其他任何方式
CREATE PROCEDURE GET_DATA
@in_filter_column1 NVARCHAR(6),
@in_filter_column2 NVARCHAR(50),
@in_flag BIT = 0
AS
BEGIN
DECLARE @sql_query NVARCHAR(MAX)
SET @sql_query = ' SELECT t1.COLUMN1, t1.COLUM2, t2.COLUMN3
FROM table1 t1
INNER JOIN table2 t2
ON t1.column1 = t2.column1'
IF @in_flag = 0
SET @sql_query = @sql_query + ' WHERE t1.column1 = ''' + @in_filter_column1 +''''
ELSE
SET @sql_query = @sql_query + ' INNER JOIN table2 t3
ON t3.column1 = t1.column1 WHERE t3.column1 = ''' + @in_filter_column2 +''''
END