/****** Object: Table [dbo].[Import_appstate] Script Date: 09/21/2013 11:10:25 ******/ IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Import_appstate]') AND type in (N'U')) DROP TABLE [dbo].[Import_appstate] GO /****** Object: Table [dbo].[Import_appstate] Script Date: 09/21/2013 11:10:25 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Import_appstate]( [id] [int] IDENTITY(1,1) NOT NULL, [ContractNo][varchar](50) not null, [app_id] [varchar](50) NOT NULL, [cargo_id] [int] not null, [weight] [numeric](18, 3) NULL, [cancellation] tinyint default 0, CONSTRAINT [PK_Import_appstate] PRIMARY KEY CLUSTERED ( [id] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO --select * from import_main insert into import_appstate values('AA01',1,1,601.32,0) insert into import_appstate values('AA01',2,5,12000,0) --select * from import_appstate select app_id ,sum([weight]) as used from import_appstate group by app_id select import_approval.*,used from import_approval left join vMsAppUsed on vMsAppUsed.app_id=import_approval.id select * from import_appstate select * from import_cargo select st.[weight],c.contractno,m.seller,m.buyer, ci.code,ci.name as ciname,c.name from import_appstate st left join import_cargo c on c.id=st.cargo_id left join Import_main m on m.contractno=c.contractno left join import_cargoinfo ci on ci.id = c.cargoinfo_id where app_id=1 select at.* , ap.*,ct.country from import_appstate at left join import_approval ap on ap.id=at.app_id left join code_country ct on ct.countryid=ap.countryid select ap.appno,at.app_id,ci.name,at.cargo_id,ca.name as cargoname, ap.[weight] appweight,au.remain appremain,ap.validdate, ct.country,convert(numeric(18,5),at.[weight]/1000) as weight from import_appstate at left join import_approval ap on ap.id=at.app_id left join code_country ct on ct.countryid=ap.countryid left join import_cargoinfo ci on ap.cargoinfoid=ci.id left join vmsappused au on au.app_id=ap.id left join import_cargo ca on ca.id=at.cargo_id where at.cargo_id in (select id from import_cargo where contractno='AA01') select ap.appno,ap.id app_id,ci.name,ap.[weight] appweight, au.remain appremain,ap.validdate ,ct.country,ci.name, ap.appno+'/'+ct.country+'/'+ci.name+'/Ê£Óà:'+convert(varchar(12),au.remain)+'¶Ö/'+ap.validdate info from import_approval ap left join import_cargoinfo ci on ap.cargoinfo_id=ci.id left join vmsappused au on au.app_id=ap.id left join code_country ct on ct.countryid=ap.countryid where ap.cargoinfo_id in(select cargoinfo_id from import_cargo where contractno='AA01') select * from import_appstate select * from import_approval select * from import_cargoinfo select * from import_cargo select * from code_country select * from vmsappused --delete from import_appstate --where app_id<>1 select at.id,at.ContractNo,ap.appno,at.app_id,ci.name,at.cargo_id,ca.name as cargoname, ap.[weight] appweight,au.remain appremain,ap.validdate, ct.country,at.[weight],at.[cancellation], (select EnumValueName from tSysEnumValue where LangId=0 and EnumTypeID=13 and EnumValueID=at.cancellation) as calcellationRef from import_appstate at left join import_approval ap on ap.id=at.app_id left join code_country ct on ct.countryid=ap.countryid left join import_cargoinfo ci on ap.cargoinfo_id=ci.id left join vmsappused au on au.app_id=ap.id left join import_cargo ca on ca.id=at.cargo_id