Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ramibn1
Starting Member
2 Posts |
Posted - 2007-04-09 : 15:15:22
|
hey all im trying to run this procedure but it's not runningand if i sperate each part it will work except the insertion part could you give me any hint thanksCREATE PROCEDURE [dbo].[SP_GM_GeographyByZone] ASdeclare @sqlstr as varchar(8000)DECLARE @RB AS VARCHAR(8000)DECLARE @RB1 AS VARCHAR(8000)DECLARE @Year AS INTif exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[dbo].[GM_GeographyByZone]') and type = 'U')BEGIN transactionTRUNCATE TABLE dbo.GM_GeographyByZoneDROP TABLE dbo.GM_GeographyByZonecommitif exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[dbo].[GM_GeographyByZone]') and type = 'U')BEGIN-- DECLARE @Year AS INT SET @Year=year(getdate()) DECLARE @tbl varchar(8000) SET @tbl = 'CREATE TABLE [dbo].[GM_GeographyByZone] ( [gmcustomeManufacturername_vc] [varchar] (50) NULL, [makedescription_vc] [varchar] (50) NULL, [' + convert(varchar(4),@year-6) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-5) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-4) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-3) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-2) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-1) + '][varchar] (4) NULL ) ON [PRIMARY]'exec(@tbl)END DECLARE @year int SET @Year=year(getdate()) declare @RB varchar(8000) SET @RB = 'INSERT INTO [dbo].[GM_GeographyByZone]( [gmcustomeManufacturername_vc] , [makedescription_vc] , [' + convert(varchar(4),@year-6) + '], [' + convert(varchar(4),@year-5) + '], [' + convert(varchar(4),@year-4) + '], [' + convert(varchar(4),@year-3) + '], [' + convert(varchar(4),@year-2) + '], [' + convert(varchar(4),@year-1) + '] )'exec(@RB)-- DECLARE @YEAR as INT SET @Year=year(getdate()) declare @sqlstr varchar(8000) SET @sqlstr = 'SELECT distinct C.makedescription_vc,C.gmcustomemanufacturername_vc, (select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-6 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-5 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-4 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-3 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-2 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-1 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') from dbo.GM_TEMP_VehicleCountByDA C order by C.makedescription_vc,C.gmcustomemanufacturername_vc' exec(@sqlstr) go |
|
X002548
Not Just a Number
15586 Posts |
|
ramibn1
Starting Member
2 Posts |
Posted - 2007-04-09 : 15:41:11
|
ok this is another copy of it and i did it this way the part that is not working is the insertion part thanks againif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[SP_GM_GeographyByZone]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[SP_GM_GeographyByZone]GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE PROCEDURE [dbo].[SP_GM_GeographyByZone] ASdeclare @sqlstr as varchar(8000)DECLARE @RB AS VARCHAR(8000)DECLARE @RB1 AS VARCHAR(8000)DECLARE @Year AS INTif exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[dbo].[GM_GeographyByZone]') and type = 'U')BEGIN transactionTRUNCATE TABLE dbo.GM_GeographyByZoneDROP TABLE dbo.GM_GeographyByZonecommitif exists (select * from tempdb.dbo.sysobjects where id = object_id(N'[dbo].[GM_GeographyByZone]') and type = 'U')BEGIN-- DECLARE @Year AS INT SET @Year=year(getdate()) DECLARE @tbl varchar(8000) SET @tbl = 'CREATE TABLE [dbo].[GM_GeographyByZone] ( [gmcustomeManufacturername_vc] [varchar] (50) NULL, [makedescription_vc] [varchar] (50) NULL, [' + convert(varchar(4),@year-6) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-5) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-4) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-3) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-2) + '][varchar] (4) NULL, [' + convert(varchar(4),@year-1) + '][varchar] (4) NULL ) ON [PRIMARY]'exec(@tbl)END-- DECLARE @year int SET @Year=year(getdate())-- declare @RB varchar(8000) SET @RB = 'INSERT INTO [dbo].[GM_GeographyByZone]( [gmcustomeManufacturername_vc] , [makedescription_vc] , [' + convert(varchar(4),@year-6) + '], [' + convert(varchar(4),@year-5) + '], [' + convert(varchar(4),@year-4) + '], [' + convert(varchar(4),@year-3) + '], [' + convert(varchar(4),@year-2) + '], [' + convert(varchar(4),@year-1) + '] )'exec(@RB)-- DECLARE @YEAR as INT SET @Year=year(getdate())-- declare @sqlstr varchar(8000) SET @sqlstr = 'SELECT distinct C.makedescription_vc,C.gmcustomemanufacturername_vc, (select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-6 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-5 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-4 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-3 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-2 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') ,(select isnull(sum(vehicleCount_in),0) gmcustomtypecode_vc from dbo.GM_TEMP_VehicleCountByDA b inner join dbo.GMSQLDBDomesticGeographyData a on a.PK_DA_vc=b.PK_DA_vc where reportyear_in=' + convert(varchar(4),@year-1 ) + ' and ZoneName_vc=''ONTARIO'' and b.makedescription_vc=C.makedescription_vc and gmprimarycode_vc=''R'' and gmcustomtypecode_vc = ''C'') from dbo.GM_TEMP_VehicleCountByDA C order by C.makedescription_vc,C.gmcustomemanufacturername_vc' exec(@sqlstr) GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GO |
|
|
|
|
|
|
|