34,590
社区成员
发帖
与我相关
我的任务
分享
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
(case when job.is_update=0 then pact.PactID = job.sync_filed else pact.Map_PactID=job.sync_filed end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact , DataJob job where job.is_update=0 and pact.PactID = job.sync_filed and job.sync_tname='PAC_InfoPact' and job.is_sync = 0
union all
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact , DataJob job where job.is_update<>0 and pact.Map_PactID=job.sync_filed and job.sync_tname='PAC_InfoPact' and job.is_sync = 0
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact' and job.is_sync = 0
union all
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
pact.Map_PactID=job.sync_filed
where job.is_update=1 and job.sync_tname='PAC_InfoPact' and job.is_sync = 0
--表结构有问题吧
--sync_tname无效
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on
job.sync_filed = (case when job.is_update=0
then pact.PactID else pact.Map_PactID end)
where job.is_sync = 0
------------------
65 H411111 0
--无视我5楼的 这样试下
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact'
union all
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.Map_PactID=job.sync_filed
where job.is_update!=0 and job.sync_tname='PAC_InfoPact'
--> 测试数据:[PAC_InfoPact]
if object_id('[PAC_InfoPact]') is not null drop table [PAC_InfoPact]
create table [PAC_InfoPact]([PactID] int,[PactCode] varchar(7),[Map_PactID] varchar(5))
insert [PAC_InfoPact]
select 65,'H411111','00650' union all
select 66,'H511111','00660'
--> 测试数据:[DataJob]
if object_id('[DataJob]') is not null drop table [DataJob]
create table [DataJob]([ID] int,[sync_filed] int,[is_update] int,[is_sync] int)
insert [DataJob]
select 1,65,0,0 union all
select 2,66,1,0
select * from [DataJob]
select * from [PAC_InfoPact]
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on
job.sync_filed = (case when job.is_update=0
then pact.PactID else pact.Map_PactID end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;
select
pact.PactID, pact.PactCode,job.is_update
from
PAC_InfoPact pact inner join DataJob job
on
job.sync_filed=(case when job.is_update=0 then pact.PactID else pact.Map_PactID end)
where
job.sync_tname='PAC_InfoPact' and job.is_sync = 0
--try
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.PactID = job.sync_filed
where job.is_update=0 and job.sync_tname='PAC_InfoPact'
union all
select pact.PactID, pact.PactCode,job.is_update
from PAC_InfoPact pact inner join DataJob job on pact.Map_PactID=job.sync_filed
where job.is_update!=0 and job.sync_tname='PAC_InfoPact'
--1. 使用 EXEC 实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 varchar(10)=null,
@para3 varchar(10)=null,
@para4 varchar(10)=null
AS
SET NOCOUNT ON
DECLARE @sql varchar(8000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
IF @para1 IS NOT NULL
SET @sql=@sql+' AND col1='''+@para1+''''
IF @para2 IS NOT NULL
SET @sql=@sql+' AND col2='''+@para2+''''
IF @para3 IS NOT NULL
SET @sql=@sql+' AND col3='''+@para3+''''
IF @para4 IS NOT NULL
SET @sql=@sql+' AND col4='''+@para4+''''
EXEC(@sql)
GO
/*======================================================*/
--2. 使用 sp_executesql 实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
DECLARE @sql nvarchar(4000)
SET @sql='SELECT * FROM tbname WHERE 1=1'
+CASE WHEN @para1 IS NULL THEN '' ELSE ' AND col1=@para1' END
+CASE WHEN @para2 IS NULL THEN '' ELSE ' AND col2=@para2' END
+CASE WHEN @para3 IS NULL THEN '' ELSE ' AND col3=@para3' END
+CASE WHEN @para4 IS NULL THEN '' ELSE ' AND col4=@para4' END
EXEC sp_executesql @sql,N'
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
',@para1,@para2,@para3,@para4
GO
/*======================================================*/
--3. 不使用动态 Transact-SQL 语句实现的动态参数存储过程
CREATE PROC p_test
@para1 varchar(10)=null,
@para2 datetime=null,
@para3 varchar(10)=null,
@para4 int=null
AS
SET NOCOUNT ON
SELECT * FROM tbname
WHERE (@para1 IS NULL OR col1=@para1)
AND (@para2 IS NULL OR col2=@para2)
AND (@para3 IS NULL OR col3=@para3)
AND (@para4 IS NULL OR col4=@para4)
-- try
select pact.PactID, pact.PactCode,job.is_update from PAC_InfoPact pact inner join DataJob job on
job.sync_filed=(case when job.is_update=0 then pact.PactID else pact.Map_PactID end)
where job.sync_tname='PAC_InfoPact' and job.is_sync = 0;