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 |
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-05-21 : 10:47:53
|
| I have a CE that has 3,816 When/Then lines. I'm getting a "Stack Space" error complaining about "optimizing the query" to fix it. Obviously, i've hit some limit here, but is the only solution to break this up into numerous CE's to get around this? |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-21 : 11:19:17
|
| Yes. And sorry, 3816? Joins man! Joins! |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-05-21 : 12:11:56
|
| it looks i might need to try that route. Even with indexes on the columns, it's taking 20 mins to execute just those CE's. kicking and screaming i go. |
 |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-05-21 : 12:15:20
|
| what the hell does that case expression do? you have over 3800 unique cases? Really? Really Really?Post it!Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-05-21 : 13:42:35
|
I have vehicle makes and models from the source agency. There are 3,800 different vehicle makes they have in their db. All are unique. I'm moving to doing an innner join of some type, but need some help with how that is constructed.The source table holds a column called MakeID which has a 3 oe 4 digit number which relates back to another table, Combineddesc which has that some number which relates to a varchar(4) value which identifies the vehicle make. In the combineddes table, the column for ReportID identifies values in there that are specific to vehicle makes.So, i'm trying to update VMAKE in CAD.dbo.CADVEHICLE based on the value in the MakeID column from Douglas.dbo.CFSVehicleData and how it relates to the ReportCode column in Combineddesc where the TypeID value in combineddesc = 136The same process held true and worked fine for setting vehicle color codes and that case expression looked like this (this was updating a different table):UPDATE cadvehicleconversionSET vcolor = CASE ( SELECT TOP 1 CD.ReportCode FROM CombinedDesc CD WHERE CD.TypeID = 137 AND CD.ID = [ColorID] ) When 'BGE' Then 'BGE'When 'BLK' Then 'BLK'When 'BLU' Then 'BLU'When 'DBL' Then 'DBL'When 'LBL' Then 'LBL'When 'BRZ' Then 'BRZ'When 'BRO' Then 'BRO'When 'COM' Then 'COM'When 'CPR' Then 'CPR'When 'CRM' Then 'CRM'When 'GLD' Then 'GLD'When 'GRY' Then 'GRY'When 'GRN' Then 'GRN'When 'DGR' Then 'DGR'When 'Lgr' Then 'LGR'When 'LAV' Then 'LAV'When 'MAR' Then 'MAR'When 'ONG' Then 'ONG'When 'PNK' Then 'PNK'When 'PLE' Then 'PLE'When 'RED' Then 'RED'When 'SIL' Then 'SIL'When 'TAN' Then 'TAN'When 'TEA' Then 'TEA'When 'TRQ' Then 'TRQ'When 'WHI' Then 'WHI'When 'YEL' Then 'YEL' ELSE '' ENDGO |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-05-21 : 14:02:05
|
the table structures for above are:CombinedDesc:CREATE TABLE [dbo].[CombinedDesc]( [ID] [int] IDENTITY(1,1) NOT NULL, [TypeID] [int] NULL, [Active] [int] NULL, [Description] [varchar](250) NULL, [ReportCode] [varchar](250) NULL, [Category] [varchar](250) NULL, [Info1] [varchar](250) NULL, [Info2] [varchar](250) NULL, CONSTRAINT [PK_CombinedDesc21_1__14] 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] CFSVehicleData:CREATE TABLE [dbo].[CFSVehicleData]( [ID] [int] IDENTITY(1,1) NOT NULL, [CFSID] [int] NULL, [VehicleID] [int] NULL, [Year] [varchar](4) NULL, [MakeID] [int] NULL, [Model] [varchar](20) NULL, [VIN] [varchar](30) NULL, [ColorID] [int] NULL, [StyleID] [int] NULL, [TagNumber] [varchar](20) NULL, [TagStateID] [int] NULL, [JurisdictionID] [int] NULL, [ActionID] [int] NULL, [Insurer] [varchar](50) NULL, [MotorSize] [varchar](20) NULL, [TransmissionID] [int] NULL, [WreckerServiceID] [int] NULL, [WreckerTime] [datetime] NULL, [TagVinToggle] [int] NULL, [WreckerLeaveTime] [datetime] NULL, [CheckForStolen] [int] NULL, [TagType] [varchar](10) NULL, [Stolen] [int] NULL, [Recovered] [int] NULL, [Suspects] [int] NULL, [Repossession] [int] NULL, [TagYear] [varchar](4) NULL, [PolicyNumber] [varchar](50) NULL, [DamageID] [int] NULL, [StolenJurisID] [int] NULL, [Speedometer] [varchar](10) NULL, [VehicleNumber] [int] NULL, [IMPORTED_DATE] [datetime] NULL, [OwnReq] [int] NULL, CONSTRAINT [PK_CFSVehicleData] PRIMARY KEY CLUSTERED (CADVehicle:CREATE TABLE [dbo].[CADVEHICLE]( [INCIDENTNO] [varchar](15) NULL, [DATE] [datetime] NULL, [TIME] [datetime] NULL, [VDESCRIPT] [varchar](30) NULL, [VSTATE] [varchar](2) NULL, [VTAG] [varchar](10) NULL, [VTAG_YEAR] [varchar](4) NULL, [VVIN] [varchar](20) NULL, [VCOLOR] [varchar](3) NULL, [VMAKE] [varchar](10) NULL, [VMODEL] [varchar](10) NULL, [VYEAR] [varchar](4) NULL, [VTYPE] [varchar](20) NULL, [VACTIVITY] [varchar](20) NULL, [CRASHTYPE] [varchar](20) NULL, [TSDR] [bit] NULL, [FLAG_TPC] [bit] NULL, [FLAG_TCN] [bit] NULL, [FLAG_TTL] [bit] NULL, [FLAG_TBO] [bit] NULL, [ABANDONED] [bit] NULL, [ABANDONED_CLEARED] [bit] NULL, [ABANDONED_DATE] [datetime] NULL, [ABANDONED_TIME] [datetime] NULL, [ABANDONED_DATE_FLAG] [datetime] NULL, [ABANDONED_TIME_FLAG] [datetime] NULL, [TOWED] [bit] NULL, [TOWED_ROTATION] [bit] NULL, [TOWED_REQUESTED] [bit] NULL, [TOWED_OTHER] [bit] NULL, [TOWED_OTHER_DESC] [varchar](20) NULL, [IS_QRESPONSE_TAG] [bit] NULL, [QRESPONSE_TAG] [text] NULL, [IS_QRESPONSE_VIN] [bit] NULL, [QRESPONSE_VIN] [text] NULL, [NOTES] [text] NULL, [ADDED_BY] [varchar](15) NULL, [UNIQUEKEY] [varchar](22) NOT NULL, [GMT_OFFSET] [int] NULL, [VOWNER_LAST] [varchar](30) NULL, [VOWNER_FIRST] [varchar](30) NULL, [VOWNER_MIDDLE] [varchar](30) NULL, [VOWNER] [varchar](90) NULL, [CICRET_TAG_FKEY] [varchar](22) NULL, [CICRET_VIN_FKEY] [varchar](22) NULL, [CALL_FKEY] [varchar](22) NULL, [MVI_POSSIBLE_MATCHES] [int] NULL, [MVI_NUMBER] [varchar](15) NULL, CONSTRAINT [PK_CADVEHICLE_UNIQUEKEY] PRIMARY KEY CLUSTERED ([/code] |
 |
|
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-21 : 14:41:59
|
I'm having trouble understanding what you're trying to update, can you provide some sample data and the results you're expecting?Some general comments:- The UPDATE you posted can be rewritten as:UPDATE CVC SET vcolor = CASE WHEN CD.ReportCode IN('BGE','BLK','BLU','DBL','LBL','BRZ','BRO','COM','CPR','CRM','GLD','GRY','GRN','DGR','LGR','LAV','MAR','ONG','PNK','PLE','RED','SIL','TAN','TEA','TRQ','WHI','YEL')THEN CD.ReportCode ELSE '' ENDFROM cadvehicleconversion CVCINNER JOIN CombinedDesc CD ON CD.TypeID = 137 AND CD.ID = CVC.[ColorID]The only reason you'd use a subquery like that is if you had multiple rows meeting the criteria, which doesn't seem likely or valid for what you're constructing. Either way TOP without an ORDER BY is meaningless and could return unintended results.- Don't use text as a data type if you're on SQL 2005 or higher, use varchar(max). And DO NOT use varchar(max) unless you are absolutely certain you need more than 8000 characters of storage for the majority of values. QRESPONSE_VIN and QRESPONSE_TAG do not strike me as such columns, if all they intend to store are VIN and tag numbers.- Similarly check the date_flag columns, if they're flags (on/off) why are they datetime? I may be misinterpreting their name and meaning, but the other flag columns are listed as bit.- Why are abandoned date and time separate columns? A single datetime column can hold both values, using 2 datetime columns wastes space. You could also use date and time data types in SQL 2008+ if you have to keep them as separate columns.- Vehicle year should be a decimal or integer datatype. If it has to be character, varchar shouldn't be used. Vehicle years are 4 digits, period. Use a smallint column, it's smaller than char/varchar and can only hold digits. You should also have a check constraint to limit the range of valid years (1880 to present year + 1).- On a related note, VSTATE should be char(2), not varchar(2). varchar(3) or less actually wastes storage space.- As I recall (and researched) VINs are fixed length of 17 characters. If data has a fixed number of characters don't make it variable length. You have CICRET_VIN_FKEY as varchar(22), that should also be reviewed.If you can modify these columns I highly recommend it, you'll have much better data quality. |
 |
|
|
WJHamel
Aged Yak Warrior
651 Posts |
Posted - 2012-05-21 : 15:00:09
|
| I do not have the luxury of changing these table structures in any way. Give me some time to chew on this a little further and i'll return if i still haven't answered my own questions.thanks |
 |
|
|
|
|
|
|
|