Would using dynamic SQL + variables speed up my query? - sql-server-2008

I've realized lately that queries are much slower when I reference a variable in the filter instead of the actual value in SQL Server 2008. For example:
SELECT table_A.fruit
, SUM(table_B.units)
FROM table_A LEFT JOIN table_B
ON table_A.fruit = table_B.fruit_name
WHERE time_of_sale BETWEEN '2012-11-01' AND '2012-11-25'
is faster than
DECLARE #date1 DATE = '2012-11-01'
, #date2 DATE = '2012-11-25'
SELECT table_A.fruit
, SUM(table_B.units)
FROM table_A LEFT JOIN table_B
ON table_A.fruit = table_B.fruit_name
WHERE time_of_sale BETWEEN #date1 AND #date2
Would changing my query to this make the query as fast as the first one I posted:
DECLARE #date1 DATE = '2012-11-01'
, #date2 DATE = '2012-11-25'
DECLARE #exec VARCHAR(8000)
SELECT #exec =
'
SELECT table_A.fruit
, SUM(table_B.units)
FROM table_A LEFT JOIN table_B
ON table_A.fruit = table_B.fruit_name
WHERE time_of_sale BETWEEN ''' + #date1 + ''' AND ''' + #date2 + '''
'
EXEC(#exec)

Related

Query run as stored procedure (sp_sqlexec) delivers no results (T-SQL, SQL Server)

I want to run a query that shows me all columns from all tables in a database with the datatype varchar and a maximum length of 8000 characters.
This is my code so far.
DECLARE #tabs VARCHAR(MAX);
SET #tabs =
(
SELECT STUFF(( SELECT DISTINCT ',' + [TABLE_NAME]
FROM [DB-Test].INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'VARCHAR' AND
CHARACTER_MAXIMUM_LENGTH = 8000
FOR XML PATH('')), 1, 1, '')
);
DECLARE #cols VARCHAR(MAX)
SET #cols =
(
SELECT STUFF(( SELECT DISTINCT ',' + [TABLE_NAME] + '.' + [COLUMN_NAME]
FROM [DB-Test].INFORMATION_SCHEMA.COLUMNS
WHERE DATA_TYPE = 'VARCHAR' AND
CHARACTER_MAXIMUM_LENGTH = 8000
FOR XML PATH('')), 1, 1, '')
);
DECLARE #query VARCHAR(MAX) = 'SELECT ' + #cols + ' FROM ' + #tabs
EXEC sp_sqlexec #query
When I run the query I get all the column names, but not the values in the columns. It's empty. No 'NULL'-values. As if #cols is interpreted as simple string maybe.
Why?
(When I read out #cols and #tabs they are correct.)
I guess there is one table available in your database which have column with VARCHAR datatype and 8000 length but that table don't have any records. Try by including only those column and table which have at least one record available.
You can try below. Check it and let me know if it works.
DECLARE #tabs VARCHAR(MAX) = ''
; WITH CTE AS
(
SELECT DISTINCT TA.NAME TABLENAME
, SUM(PA.ROWS) OVER (PARTITION BY TA.NAME ) NOOFROW
FROM SYS.TABLES TA
INNER JOIN SYS.PARTITIONS PA ON PA.OBJECT_ID = TA.OBJECT_ID
INNER JOIN SYS.SCHEMAS SC ON TA.SCHEMA_ID = SC.SCHEMA_ID
WHERE TA.IS_MS_SHIPPED = 0 AND PA.INDEX_ID IN (1,0)
), TABLENAME AS
(
SELECT ITBL.[TABLE_NAME]
FROM INFORMATION_SCHEMA.COLUMNS ITBL
WHERE ITBL.DATA_TYPE = 'VARCHAR' AND
ITBL.CHARACTER_MAXIMUM_LENGTH = 8000
AND EXISTS(SELECT 1 FROM CTE WHERE CTE.TABLENAME = ITBL.TABLE_NAME AND CTE.NOOFROW > 0) -- To check no of record available in table
)
SELECT #tabs = #tabs+ISNULL(','+TABLE_NAME, '')
FROM TABLENAME
DECLARE #cols VARCHAR(MAX) = '';
; WITH CTE AS
(
SELECT DISTINCT TA.NAME TABLENAME
, SUM(PA.ROWS) OVER (PARTITION BY TA.NAME ) NOOFROW
FROM SYS.TABLES TA
INNER JOIN SYS.PARTITIONS PA ON PA.OBJECT_ID = TA.OBJECT_ID
INNER JOIN SYS.SCHEMAS SC ON TA.SCHEMA_ID = SC.SCHEMA_ID
WHERE TA.IS_MS_SHIPPED = 0 AND PA.INDEX_ID IN (1,0)
), TABLENAME AS
(
SELECT ITBL.[TABLE_NAME], ITBL.[COLUMN_NAME]
FROM INFORMATION_SCHEMA.COLUMNS ITBL
WHERE ITBL.DATA_TYPE = 'VARCHAR' AND
ITBL.CHARACTER_MAXIMUM_LENGTH = 8000
AND EXISTS(SELECT 1 FROM CTE WHERE CTE.TABLENAME = ITBL.TABLE_NAME AND CTE.NOOFROW > 0) -- To check no of record available in table
)
SELECT #cols = #cols+ISNULL(','+[TABLE_NAME]+'.'+[COLUMN_NAME], '')
FROM TABLENAME
IF LEN(#cols) > 0 AND LEN(#tabs) > 0
BEGIN
DECLARE #query VARCHAR(MAX) = 'SELECT ' + STUFF(#cols,1,1,'') + ' FROM ' + STUFF(#tabs,1,1, '')
EXEC sp_sqlexec #query
END
ELSE
BEGIN
PRINT 'No Column available with data where it''s datatype is VARCHAR and length is 8000'
END

List dates between two date range

I have table 1 with 3 columns id, startdate and enddate. With order id being the primary key how do I list the dates between the date range Startdate and Enddate?
What I have:
id Startdate EndDate
1 2/11/2014 2/13/2014
2 2/15/2014 2/17/2014
What I need:
id Date
1 2/11/2014
1 2/12/2014
1 2/13/2014
2 2/15/2014
2 2/16/2014
2 2/17/2014
How do I do this?
Use recursive CTE:
WITH tmp AS (
SELECT id, StartDate AS [Date], EndDate
FROM MyTable
UNION ALL
SELECT tmp.id, DATEADD(DAY,1,tmp.[Date]), tmp.EndDate
FROM tmp
WHERE tmp.[Date] < tmp.EndDate
)
SELECT tmp.ID, tmp.[Date]
FROM tmp
ORDER BY tmp.id, tmp.[Date]
OPTION (MAXRECURSION 0) -- For long intervals
If you have to use cursor/loop, most times you are doing it wrong.
If you do a one-off setup of an auxiliary calendar table as shown at Why should I consider using an auxiliary calendar table?, possibly omitting a lot of the columns if you don't need them, like this:
CREATE TABLE dbo.Calendar
(
dt SMALLDATETIME NOT NULL
PRIMARY KEY CLUSTERED,
Y SMALLINT,
M TINYINT,
D TINYINT
)
GO
SET NOCOUNT ON
DECLARE #dt SMALLDATETIME
SET #dt = '20000101'
WHILE #dt < '20300101'
BEGIN
INSERT dbo.Calendar(dt) SELECT #dt
SET #dt = #dt + 1
END;
UPDATE dbo.Calendar SET
Y = YEAR(dt),
M = MONTH(dt),
D = DAY(dt);
(You may well not need the Y, M, D columns at all, but I left those in to show that more data can be stored for fast access - the article I linked to shows how that could be used.)
Then if your table is named "so", your code would simply be
SELECT A.id, C.dt
FROM so AS A
JOIN Calendar AS C
ON C.dt >= A.StartDate AND C.dt<= A.EndDate
An advantage of using an auxiliary table like that is that your queries can be faster: the work done in setting one up is a one-time cost which doesn't happen during usage..
Instead of using CTE (to over come recursive and performance when date range is large) below query can be used to get the list of dates between two date range.
DECLARE #StartDateSTR AS VARCHAR(32); DECLARE #EndDateSTR AS
VARCHAR(32); DECLARE #EndDate AS DATE; DECLARE #StartDate AS DATE;
SET #StartDateSTR = '01/01/1990'; SET #EndDateSTR = '03/31/2025'; SET
#StartDate = CAST(#StartDateSTR AS date); SET #EndDate =
cast(#EndDateSTR AS date); SELECT
DATEADD(DAY, n1.rn - 1, #StartDate) AS dt FROM (SELECT rn=Row_number() OVER( ORDER BY (SELECT NULL)) FROM sys.objects a
CROSS JOIN sys.objects b CROSS JOIN sys.objects c CROSS JOIN
sys.objects d) as n1 WHERE n1.[rn] <= Datediff(dd, #StartDate,
#EndDate)+1;

Parameter is missing a value

May you please assist me with this issue
I have created a report that receives 5 parameters
#Site - Location Name
#StartDate
#EndDate
#StartTime
#EndTime
All seems to be working fine, the report filters according to what is required. The problem is now the same rdl file I need to publish it on another system and emmediately after publishing when I run it gives me this error "Reportviewer - specific report error: The 'StartTime' parameter is missing a value ;0;Void Error(System.String, System.Object[])"
Am not sure where to look into at this moment as I don't experience the error when running report on my local. Below is the code I used:
Parameters
#StartDate default = dateadd(dateinterval.Day, -1, Today)
#EndDate default = =dateadd(dateinterval.Day, -1, Today)
#SiteID SQL Dataset = SELECT 0 AS ID, 'All' as Name
FROM sites
UNION
SELECT ID, Name
FROM sites
#StartTime default = 12:00:00 AM
Available values (12:00:00 AM - 11:00:00 PM
#EndTime default = 11:00:00 M
Available values (12:00:00 AM - 11:00:00 PM
SQL Dataset
Declare #Start datetime = cast (#StartDate + ' '+ #StartTime as datetime)
Declare #End datetime = cast(#EndDate + ' ' + #EndTime as datetime )
SELECT Distinct ast.Name as HomeSite, ISNULL(c.FirstName + ' ', '') + c.LastName as Name, md.MemRefNo,att2.Name as VisitedSite,isnull(att2.TotalVisits,0) as TotalVisits, isnull(atc.totalaccepted,0) TotalAcceptedVisits, ISNULL(att.TotalOverrideVisits, 0) AS TotalOverrideVisits, isnull(att1.TotalOverrideDenieds,0) as TotalOverrideDenieds
FROM Contacts c
INNER JOIN MemberDetail md on md.ContactGUID = c.GUID
INNER JOIN Sites ast on c.HomeSiteID = ast.ID
OUTER APPLY
(
SELECT ast.Name,a1.contactguid,COUNT(*) totalaccepted
FROM Attendance a1
INNER JOIN Sites ast on a1.SiteID = ast.ID
WHERE a1.contactguid = c.GUID
AND (a1.IsSwipeSuccessful = 1)
AND a1.accessoverridereasonid IS NULL
AND a1.AttendDate BETWEEN #Start AND #End
AND (a1.SiteID = #SiteID OR #SiteID = 0)
group by a1.contactguid,ast.Name
)atc
OUTER APPLY
(
SELECT ast.Name,a2.contactguid,COUNT(*) TotalOverrideVisits
FROM Attendance a2
INNER JOIN Sites ast on a2.SiteID = ast.ID
WHERE a2.contactguid = c.GUID
AND a2.accessoverridereasonid IS NOT NULL
AND a2.AttendDate BETWEEN #Start AND #End
AND (a2.SiteID = #SiteID OR #SiteID = 0)
group by a2.contactguid,ast.Name
) att
Outer APPLY
(
SELECT ast.Name,a3.contactguid,COUNT(*) TotalOverrideDenieds
FROM Attendance a3
INNER JOIN Sites ast on a3.SiteID = ast.ID
WHERE a3.contactguid = c.GUID
AND a3.IsSwipeSuccessful = 0
AND a3.AttendDate BETWEEN #Start AND #End
AND (a3.SiteID = #SiteID OR #SiteID = 0)
group by a3.contactguid,ast.Name
) att1
Cross APPLY
(
SELECT ast.Name,a3.contactguid,COUNT(*) TotalVisits
FROM Attendance a3
INNER JOIN Sites ast on a3.SiteID = ast.ID
WHERE a3.contactguid = c.GUID
AND a3.AttendDate BETWEEN #Start AND #End
AND (a3.SiteID = #SiteID OR #SiteID = 0)
group by a3.contactguid,ast.Name
) att2
Order by name
Try refreshing all the datasets (shared and non-shared)
Check their properties (especially the parameters page) and make
sure they are right.
Redeploy (make sure you are overwrite existing datasets)
Hope this helps,
Henro

what is causing error (operands should contain 1 column)

I am trying to set myItemId so that I can use it in the concat query. Everything works fine until I add this row
SET myItemID = (SELECT * FROM items i WHERE i.name LIKE '%KW PRE FREE COOLING%');
It then gives me an error of
Operand should contain 1 column(s)
Here is the query that I am working with
CREATE PROCEDURE reportFreeCoolingTempTable (
IN fromDate VARCHAR (50),
IN toDate VARCHAR (50),
IN timeZone VARCHAR (50)
)
BEGIN
DECLARE startDate VARCHAR (50);
DECLARE endDate VARCHAR (50);
DECLARE mylogID INT;
DECLARE myItemID int;
SET startDate = FROM_UNIXTIME(fromDate/1000);
SET endDate = FROM_UNIXTIME(toDate/1000);
SET mylogID = (SELECT logID FROM logs WHERE details LIKE 'FCT%' LIMIT 1);
SET myItemID = (SELECT * FROM items i WHERE i.name LIKE '%KW PRE FREE COOLING%');
SET #sql = NULL;
SET #sql = NULL;
SET #sql = CONCAT(
'SELECT #row:=#row+1 as rownum,
a.logid ,
L1.recordId,
L2.recordId as next_recordId,
L1.completed,
L2.completed as next_completed,
L1.activityId,
L2.activityId as next_activityId,
IF(L1.activityId = L2.activityId,1,NULL) as isError,
TIME_TO_SEC(TIMEDIFF(L2.completed, L1.completed)) / 3600 AS coolingHours,
((L1.item31985 - L1.item31987) * (time_to_sec(timediff(L2.completed, L1.completed)))) / 3600 AS kwDifference,
((L1.item31985 - L1.item31987) * (substr(l.details, instr(l.details , '':'' ) +1))) AS cost,
( (((L1.item31985 - L1.item31987) * (substr(l.details, instr(l.details , '':'' ) +1)))
*(time_to_sec(timediff(L2.completed, L1.completed)) / 3600))) AS costT,
time_to_sec(timediff(''', endDate, ''', ''', startDate, ''')) / 3600 AS totalTimeRange,
CONVERT_TZ(''', startDate, ''', ''UTC'', ''', timeZone, ''') AS startingDate,
CONVERT_TZ(''', endDate, ''', ''UTC'', ''', timeZone, ''') AS endingDate,
DATABASE() AS databaseName
FROM
(SELECT #row:=0)R,
(SELECT T1.completed,
(SELECT MIN(completed)
FROM log1644
WHERE completed > T1.completed) AS next_completed
FROM log',mylogID, ' T1
ORDER BY T1.completed
)TimeOrder
LEFT JOIN log', mylogID, ' L1 ON (L1.completed = TimeOrder.completed)
LEFT JOIN log', mylogID, ' L2 ON (L2.completed = TimeOrder.next_completed)
LEFT JOIN activities a ON L1.activityId = a.activityId
LEFT JOIN logs l ON a.logId = l.logId
Left Join items i ON l.logId = i.logId AND i.name LIKE ''%KW%''
WHERE i.itemID = 31985
AND L1.completed BETWEEN ''', startDate, ''' AND ''', endDate, '''
ORDER BY L1.completed');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END //
DELIMITER ;
Error itself explains (operands should contain 1 column) you need to select the single column from the query in order to set myItemID ,you are selecting all the columns from the items try this
SET myItemID = (SELECT id FROM items i WHERE i.name LIKE '%KW PRE FREE COOLING%' LIMIT 1 );
I assume the you need to set the myItemID to the id column from items where you conditions matches.i have also added LIMIT 1 in order to avoid the error of subquery should return one result
The error is caused because the SET statement expects a single value to be returned from your subquery. Not only can it return multiple values (SELECT *), but it can potentially return multiple rows. Change your query to specify just the single column from your subquery that you want to assign to myItemId, and ensure that it can return only 1 row - like this:
SET myItemID = (SELECT TOP 1 [itemIdColumnName] FROM items i WHERE i.name LIKE '%KW PRE FREE COOLING%');
The 'operand' in your case is "myItemID". It can only hold ONE value. Your SELECT statement returns all the rows in the table (multiple columns). You need to select only the 1 column that represents the ID you are trying to obtain.

Loop through all records in my SQL Server database

I have the following SQL script. As you can see I manually set the #listingid value to 30653.
But this script should be executed for all records in the [listings] table where #listingid is assigned the value of the [listings].id column.
DECLARE #profname nvarchar(150)
DECLARE #furl nvarchar(250)
DECLARE #city nvarchar(250)
DECLARE #listingid int
set #listingid=30653
--select the top 1 professionname
SELECT TOP 1 #profname=REPLACE(LOWER(pn.title),' ','-'),#furl=l.friendlyurl,#city=REPLACE(REPLACE(LOWER(l.city),'''',''),' ','-') FROM healthprof_professionnames hpn
INNER JOIN professionname pn ON pn.id=hpn.professionnameid
INNER JOIN listings l on l.id=hpn.healthprofid
WHERE l.id=#listingid ORDER BY pn.title
--check if current friendlyurl already contains profession
IF NOT CHARINDEX(#profname,#furl)>0
SET #furl = #furl + '-' + #profname
IF NOT CHARINDEX(#city,#furl)>0
SET #furl = #furl + '-' + #city
SET #furl = #furl + '-3'
UPDATE listings set friendlyurl=#furl WHERE id=#listingid
You can use a cursor to loop over every row in a result set:
declare cur cursor for
select distinct id from listings
declare #listingid int
open cur
fetch next from cur into #listingid
while ##FETCH_STATUS = 0
BEGIN
-- your code from above goes here
fetch next from cur into #listingid
END
That being said, I agree with Tim's comment above. Rewrite it to work in one set-based operation if at all possible. I think this will work, but I haven't tested it:
;WITH vars AS (
SELECT id, profname, furl, city
FROM (
SELECT l.id,
REPLACE(LOWER(pn.title),' ','-') as profname,
l.friendlyurl as furl,
REPLACE(REPLACE(LOWER(l.city),'''',''),' ','-') as city,
ROW_NUMBER() OVER (PARTITION BY l.id ORDER BY pn.title) as rnk
FROM healthprof_professionnames hpn
INNER JOIN professionname pn ON pn.id=hpn.professionnameid
INNER JOIN listings l on l.id=hpn.healthprofid
) A
WHERE A.rnk = 1
),
vars2 AS (
SELECT id,
CASE WHEN NOT CHARINDEX(profname, furl) > 0
THEN furl + '-' + profname ELSE furl END as furl,
city
FROM vars
),
vars3 as (
SELECT id,
CASE WHEN NOT CHARINDEX(city, furl) > 0
THEN furl + '-' + city ELSE furl END as furl
FROM vars2
)
UPDATE listings SET friendlyurl = vars3.furl + '-3'
FROM listings INNER JOIN vars3 on vars3.id = listings.id