SQLTeam.com | Weblogs | Forums

Need help to combine 2 SQL Statement


#1

Hello,

I've Table and Data as following,

CREATE TABLE [dbo].[crpt_LembaranImbangan_2_Param_2](
    [idx] [int] IDENTITY(-2147483648,1) NOT NULL,
    [column_Nme] [nvarchar](100) NULL,
    [Comparison_Operator_Code] [nvarchar](50) NULL,
    [what_Value] [nvarchar](5) NULL
) ON [PRIMARY]
GO
SET IDENTITY_INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ON
INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ([idx], [column_Nme], [Comparison_Operator_Code], [what_Value]) VALUES (-2147483648, N'chart_code', N'>=', N'32000')
INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ([idx], [column_Nme], [Comparison_Operator_Code], [what_Value]) VALUES (-2147483647, N'chart_code', N'<=', N'36000')
INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ([idx], [column_Nme], [Comparison_Operator_Code], [what_Value]) VALUES (-2147483646, N'chart_code', N'<', N'33***')
INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ([idx], [column_Nme], [Comparison_Operator_Code], [what_Value]) VALUES (-2147483645, N'chart_code', N'!=', N'37000')
INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] ([idx], [column_Nme], [Comparison_Operator_Code], [what_Value]) VALUES (-2147483644, N'chart_code', N'<', N'4000')
SET IDENTITY_INSERT [dbo].[crpt_LembaranImbangan_2_Param_2] OFF
/****** Object:  Default [DF_crpt_LembaranImbangan_2_Param_2_column_Nme]    Script Date: 11/18/2015 09:00:07 ******/
ALTER TABLE [dbo].[crpt_LembaranImbangan_2_Param_2] ADD  CONSTRAINT [DF_crpt_LembaranImbangan_2_Param_2_column_Nme]  DEFAULT ('chart_code') FOR [column_Nme]
GO
/****** Object:  Check [CK_crpt_LembaranImbangan_2_Param_2]    Script Date: 11/18/2015 09:00:07 ******/
ALTER TABLE [dbo].[crpt_LembaranImbangan_2_Param_2]  WITH CHECK ADD  CONSTRAINT [CK_crpt_LembaranImbangan_2_Param_2] CHECK  (([column_Nme]='chart_code'))
GO
ALTER TABLE [dbo].[crpt_LembaranImbangan_2_Param_2] CHECK CONSTRAINT [CK_crpt_LembaranImbangan_2_Param_2]
GO

Let's say, I've Dynamic SQL as following,

 Declare @sql nvarchar(max); Set @sql= 'select ''batch_Id'', chart_code, pusat_kos  from crpt_LembaranImbangan_1
where 1=1 ';

/* need combine the filter expression from [dbo].[crpt_LembaranImbangan_2_Param_2]. Please assume the combination operators is - OR

idx    column_Nme    Comparison_Operator_Code    what_Value
------------------------------------------------------------------
-2147483648    chart_code    >=    32000
-2147483647    chart_code    <=    36000
-2147483646    chart_code    <    33***
-2147483645    chart_code    !=    37000
-2147483644    chart_code    <    4000
*/

Exec(@sql);

After Exec(@sql), the result with conditions similar as follow,

Please help