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 |
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-09-12 : 03:10:59
|
I have to do some searching on my web forms.There are numbers of parameters where user can fill info.What i am doing is that if someone not provided the value i put a default value (which i know will not provide will not evaluate)My stored procedure isALTER PROCEDURE [dbo].[uspGetStandAloneDashboardInfo] @MailClass VarChar(3), @MailShape VarChar(3), @AssociatedToAppointment VarChar(5), @InductionDate VarChar(8), @ConsigneeApptID VarChar(12), @LocaleKey VarChar(9), @MailPreparerID VarChar(12), @ConsigneeContentID VarChar(12), @LatestTransactionStatus VarChar(75)AS BEGIN SET NOCOUNT ON; SELECT * FROM STANDALONE_CONTENT_INFO sinfo INNER JOIN HIST_STANDALONE_CONTENT_INFO hsInfo ON sinfo.SAC_SchedulerContentID = hsInfo.SAC_SchedulerContentID Where ( @MailClass = '-1-' OR sinfo.SAC_MailClass = @MailClass ) And ( @LatestTransactionStatus = '-1-' or hsInfo.SAC_Status = @LatestTransactionStatus ) And ( @MailShape = '-1-' OR sinfo.SAC_ProcessingCategory = @MailShape ) And ( @ConsigneeContentID = '-1-' OR hsinfo.SAC_ConsigneeContentID like '%' + @ConsigneeContentID + '%' ) And ( @LocaleKey = '-1-' OR sinfo.SAC_LocaleKey like '%' + @LocaleKey + '%' ) And ( @MailPreparerID = '-1-' OR sinfo.SAC_MailPreparerID like '%' + @MailPreparerID + '%' ) ENDIn it i were thinking that in the where clauseAnd ( @MailPreparerID = '-1-' OR sinfo.SAC_MailPreparerID like '%' + @MailPreparerID + '%' )as i provide @MailPreparerID = '-1-' so the condition sinfo.SAC_MailPreparerID like '%' + @MailPreparerID + '%' will not be evaluatedPlease correct me and also suggest me any alternativeI have followed technique like http://archive.msdn.microsoft.com/SQLExamples/Wiki/View.aspx?title=NonDynamicSearchKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net)[url][/url][url][/url] |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-09-12 : 03:43:22
|
following is my real stored procedureUSE [AimsDBOfVertis]GO/****** Object: StoredProcedure [dbo].[uspGetPartnerAppointmentDashboardInfo] Script Date: 09/12/2012 12:39:43 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOALTER PROCEDURE [dbo].[uspGetPartnerAppointmentDashboardInfo] @ConsigneeApptID as varchar(12) ,@LocaleKey as varchar(10) ,@QueryStatus as varchar(15) ,@PartnerID as int ,@RecurringAppointmentStatus as varchar(20) ,@JobID as varchar(8) ,@JobName as varchar(30) ,@UserLabel1 as varchar(40) ,@UserLabel2 as varchar(40) ,@ScheduledApptStartDate as datetime ,@ScheduledApptEndDate as datetime ,@FacilityName as varchar(50) ,@SchedulerID as varchar(12) ,@CRID as varchar(15) ,@CloseOutStatus as varchar(20) ,@TruckArrivalDate as datetime ,@TruckUnloadStart as datetime ,@TruckUnloadEnd as datetimeASBEGINSET TRANSACTION ISOLATION LEVEL SNAPSHOT SET NOCOUNT ON; WITH XMLNAMESPACES('http://idealliance.org/maildat/Specs/md091/mailxml80B/mailxml' AS mailxml)Select distinct HeaderRecord.hdrOriginalFlatFileJobID, HeaderRecord.hdrJobNameTitleAndIssue, csmLabelUserInformationLine1, csmLabelUserInformationLine2, paqr.ConsigneeApptID, --HeaderRecord.fkJobID, PartnerProfile.PartnerID, PartnerProfile.PartnerName, paqr.QueryRequestID, paqr.AppointmentQueryStatus, paqr.PartnerSchedulerCorpID, paqr.PartnerSchedulerID, paqr.PartnerSchedulerCRID, paqr.LastQueriedAt, paqr.TrackingID, paqr.FacilityName, paqr.FacilityNumber, paqr.TrailerLength, paqr.ScheduledAppt, paqr.ContentCount, paqr.EffectiveStartDate, paqr.EffectiveEndDate, paqr.ApptTime, paqr.DayFrequency, paqr.WeekFrequency, paqr.RecurringAppointmentStatus, paqr.QueryType, paqr.PartnerAppointmentQueryStartDate, paqr.PartnerAppointmentQueryEndDate, CONVERT(DATETIME2, Closeout_Info.TruckArrival) as FASTTruckArrival, CONVERT(DATETIME2, Closeout_Info.UnloadStart) as FASTTruckUnloadStart, CONVERT(DATETIME2, Closeout_Info.UnloadEnd) as FASTTruckUnloadEnd, Closeout_Info.[Status] as AppointmentCloseoutStatus, CASE WHEN (select COUNT( Cast(Y.Barcode as varchar(max)))) > 0 THEN (select COUNT( Cast(Y.Barcode as varchar(max)))) WHEN (select COUNT( Cast(Y.Barcode as varchar(max)))) = 0 THEN NULL END as ContainerCount FromPartnerAppointmentQueryResponse paqr Inner Join PartnerProfile ON PartnerProfile.PartnerID = paqr.PartnerID left outer join ( SELECT T.[ConsigneeApptID] as ApptID, T.QueryRequestID as WithBarcodeQueryRequestID, x.i.query('data(.)') as Barcode FROM dbo.PartnerAppointmentQueryResponse AS T CROSS APPLY T.PartnerApptQueryResponseType.nodes('/mailxml:PartnerApptQueryResponse/mailxml:QueryResults/mailxml:ApptBlock/mailxml:ContentDetailInfoUpdate/mailxml:ContentUSPSSummary/mailxml:Pallets//mailxml:USPSContainerInfo/mailxml:UniqueContainerBarcode') AS x(i) WHERE PartnerApptQueryResponseType is not null And ContentCount > 0 ) Y ON Y.ApptID = paqr.ConsigneeApptID And paqr.QueryRequestID = Y.WithBarcodeQueryRequestIDLEFT OUTER Join ContainerSummaryRecord on Cast(Y.Barcode as varchar(max)) = ContainerSummaryRecord.csmLabelIMContainerOrIMTrayBarcode and csmIsDeleted = 0 and ContainerSummaryRecord.csmLabelIMContainerOrIMTrayBarcode is not null And Cast(Y.Barcode as varchar(max)) is not null LEFT OUTER Join HeaderRecord on HeaderRecord.fkJobID = ContainerSummaryRecord.fkJobID and HeaderRecord.hdrHistoryStatus = 'C' and HeaderRecord.hdrIsDeleted = 0LEFT OUTER JOIN Closeout_Info ON paqr.ConsigneeApptID = Closeout_Info.ConsigneeID and paqr.ConsigneeApptID is not null where paqr.PartnerID = PartnerProfile.PartnerID and (@ConsigneeApptID = '-1-' OR paqr.ConsigneeApptID like '%'+ @ConsigneeApptID +'%') and (@LocaleKey = '-1-' OR paqr.FacilityNumber like '%'+ @LocaleKey +'%') and (@FacilityName = '-1-' OR paqr.FacilityName like '%'+ @FacilityName +'%') and (@QueryStatus = '-1-' OR paqr.AppointmentQueryStatus = @QueryStatus) and (@CRID = '-1-' OR paqr.PartnerSchedulerCRID like '%'+ @CRID +'%') and (@SchedulerID = '-1-' OR paqr.PartnerSchedulerID like '%'+ @SchedulerID +'%') and (@RecurringAppointmentStatus = '-1-' OR paqr.RecurringAppointmentStatus = @RecurringAppointmentStatus) and (@PartnerID IS NULL OR PartnerProfile.PartnerID = @PartnerID) And (@UserLabel1 ='-1-' or ContainerSummaryRecord.csmLabelUserInformationLine1 like '%'+ @UserLabel1 +'%') and (@UserLabel2 ='-1-' or ContainerSummaryRecord.csmLabelUserInformationLine2 like '%'+ @UserLabel2 +'%') and (@JobID ='-1-' or HeaderRecord.hdrOriginalFlatFileJobID like '%'+ @JobID +'%') and (@JobName ='-1-' or HeaderRecord.hdrJobNameTitleAndIssue like '%'+ @JobName +'%') and (@CloseOutStatus ='-1-' or Closeout_Info.[Status] like '%'+ @CloseOutStatus +'%') and (@ScheduledApptStartDate is null or paqr.ScheduledAppt >= @ScheduledApptStartDate ) and (@ScheduledApptEndDate is null or paqr.ScheduledAppt <= @ScheduledApptEndDate ) and (@TruckArrivalDate Is null OR Convert(date,Closeout_Info.TruckArrival) = Convert(date,@TruckArrivalDate)) and (@TruckUnloadStart Is null OR Convert(date,Closeout_Info.UnloadStart) = Convert(date,@TruckUnloadStart)) and (@TruckUnloadEnd Is null OR Convert(date,Closeout_Info.UnloadEnd) = Convert(date,@TruckUnloadEnd)) group by HeaderRecord.hdrOriginalFlatFileJobID,HeaderRecord.hdrJobNameTitleAndIssue, csmLabelUserInformationLine1, csmLabelUserInformationLine2, paqr.ConsigneeApptID, -- HeaderRecord.fkJobID, PartnerProfile.PartnerID, PartnerProfile.PartnerName, paqr.QueryRequestID, paqr.AppointmentQueryStatus, paqr.PartnerSchedulerCorpID, paqr.PartnerSchedulerID, paqr.PartnerSchedulerCRID, paqr.LastQueriedAt, paqr.TrackingID, paqr.FacilityName, paqr.FacilityNumber, paqr.TrailerLength, paqr.ScheduledAppt, paqr.ContentCount, paqr.EffectiveStartDate, paqr.EffectiveEndDate, paqr.ApptTime, paqr.DayFrequency, paqr.WeekFrequency, paqr.RecurringAppointmentStatus, paqr.QueryType, paqr.PartnerAppointmentQueryStartDate, paqr.PartnerAppointmentQueryEndDate, Closeout_Info.TruckArrival,Closeout_Info.UnloadStart,Closeout_Info.UnloadEnd,Closeout_Info.[Status]ENDKamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
chadmat
The Chadinator
1974 Posts |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-09-12 : 05:10:40
|
then any alternative?Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
kamii47
Constraint Violating Yak Guru
353 Posts |
Posted - 2012-09-12 : 05:41:54
|
I am trying dynamic sqlbut having problem at if( @TruckArrivalDate is not null ) begin set @strwhereClause = @strwhereClause + ' and Convert(date,Closeout_Info.TruckArrival) = ' + Convert(date, @TruckArrivalDate) end if( @TruckUnloadStart is not null ) begin set @strwhereClause = @strwhereClause + ' and Convert(date,Closeout_Info.UnloadStart) = ' + Convert(date, @TruckUnloadStart) end if( @TruckUnloadEnd is not null ) begin set @strwhereClause = @strwhereClause + ' and Convert(date,Closeout_Info.UnloadEnd) = ' + Convert(date, @TruckUnloadEnd) endMsg 402, Level 16, State 1, Procedure uspGetPartnerAppointmentDashboardInfoTemp, Line 138The data types varchar(max) and date are incompatible in the add operator.Msg 402, Level 16, State 1, Procedure uspGetPartnerAppointmentDashboardInfoTemp, Line 145The data types varchar(max) and date are incompatible in the add operator.Msg 402, Level 16, State 1, Procedure uspGetPartnerAppointmentDashboardInfoTemp, Line 152The data types varchar(max) and date are incompatible in the add operator.Kamran ShahidPrinciple Engineer Development(MCSD.Net,MCPD.net) |
 |
|
chadmat
The Chadinator
1974 Posts |
Posted - 2012-09-12 : 12:41:04
|
You can't add a datetim into a string, it needs to be varchar.-Chad |
 |
|
|
|
|
|
|