Monday, September 30, 2013

[T-SQL] Usage of OR in WHERE clause

[T-SQL] Usage of OR in WHERE clause


Usage of OR in WHERE clause

Posted: 30 Sep 2013 12:47 AM PDT

Apologies for a silly question, but I was taught in the past to avoid usage of OR in WHERE clause as it effectedSELECT's performance and ordered SQL engine to re-scan the table, so I keep avoiding since.Is it a same with SQL 2008 and higher and are there any guidelines / best practice please, e.g. I use IN or CASE where possible?Thank you.

Error arthematic operations inside varchar @variable

Posted: 29 Sep 2013 10:43 PM PDT

Hi I'm getting conversion error in the messages while executing below query.Please help me understand this. [code="sql"]DECLARE @Linkedserver sysname = 'DataServerSeven';DECLARE @Databasename sysname = 'dbVinnyStaging';Declare @DeltaSizeInMB BIGINTDeclare @EDWSourceSystemID INT = 4100Declare @SourceSystemID INT = 4100DECLARE @DeltaSize Table (DeltaSize BIGINT)Declare @DeltaSizeSQL Varchar(MAX)Declare @TempBatchNumber INT = 1736Set @DeltaSizeSQL = 'Select SUM(A.DeltaCount * B.AvgRecordSize) from '+@Linkedserver+'.'+@Databasename+'.dbo.parameterhistory A Join Operations..DatasetTables B (Nolock) ON A.ReferenceObjectName = B.TableName where A.SourceExecutionKey in ('+@TempBatchNumber+') and A.SourceSystemID in ('+@SourceSystemID+') and B.EDWSourceSystemID in ('+@EDWSourceSystemID+');'Insert INTO @DeltaSize EXECUTE sp_executesql @DeltaSizeSQL SelecT @DeltaSizeInMB = (Select sum(cast(DeltaSize AS Numeric(15,4))/1024/1024) from @DeltaSize) select @DeltaSizeInMB[/code][code="sql"]Msg 245, Level 16, State 1, Line 9Conversion failed when converting the nvarchar value 'Select SUM(A.DeltaCount * B.AvgRecordSize) from DataServerSeven.dbVinnyStaging.dbo.parameterhistory A Join Operations..DatasetTables B (Nolock) ON A.ReferenceObjectName = B.TableName where A.SourceExecutionKey in (' to data type int.[/code]

Update script

Posted: 29 Sep 2013 08:21 PM PDT

Hi,I a mtrying to create update staments including data for the table in this way but I am getting error [i]Incorrect syntax near '+'.[/i]Can anyone tell me whats wrongs in this script?[code="sql"] SELECT 'UPDATE table_1SET labeltext ='+ T.labeltext 'WHERE LanguageID = 10AND LabelKey ='+ T.LabelKey 'AND FileID =' + T.FileID[/code]

No comments:

Post a Comment

Search This Blog