I have store procedure like this:
ALTER procedure [dbo].[performance] #startdate nvarchar(100),
#enddate nvarchar(100) as
begin
declare #date1 nvarchar(100)=convert(varchar, #startdate+'00:00:00.000',120)
declare #date2 nvarchar(100)= convert(varchar, #enddate+'23:59:59.000',120)
set NOCOUNT on;
select l.LocName,v.Vtype,
SUM(convert(numeric(18, 2),
DATEDIFF(MI,t.DelDate,t.Paydate))) as TotalDiff,
[dbo].[testfunctionstacknew]
(CONVERT(decimal(10,1),
AVG( CONVERT(NUMERIC(18,2),
DATEDIFF(SS,t.Paydate,t.DelDate) ) ))
) as Average
from Transaction_tbl t
left join VType_tbl v on t.vtid=v.vtid
left join Location_tbl l on t.Locid=l.Locid
where t.Locid in (select t1.Locid from Transaction_tbl t1) and
dtime between #date1 and #date2 and
Status =5
group by v.Vtype,l.LocName,l.Locid
order by l.Locid
end
I am getting out put like this:
LocName Vtype TotalDiff Average
Address Normal 15 00:10:01
Adress vip 18 00:08:01
Address VVIP 9 00:04:00
Address Pass 20 00:15:00
Goldsouk normal 45 00:18:08
Goldsouk vip 17 00:11:36
Fashion vip 78 00:35:25
Fashion VVip 2 00:01:00
but i need output in different model as
LocName Normal Vip VVip Pass Staff
Address 00:10:01 00:08:01 00:04:00 0 0
GoldSouck 00:18:08 00:11:36 0 0 0
Fashion 0 00:35:25 00:01:00 0 0
so I try to write the same stored procedure by using pivot
ALTER procedure [dbo].[ParkingSummary1]
#startdate nvarchar(100), #enddate nvarchar(100) as
begin
declare #date1 nvarchar(100) = convert(varchar, #startdate+' 00:00:00.000', 120)
declare #date2 nvarchar(100) = convert(varchar, #enddate+' 23:59:59.000', 120)
DECLARE #cols AS NVARCHAR(MAX),#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype) from VType_tbl
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set #query = 'SELECT LocName, ' + #cols + ' from (select l.LocName,Vtype from Transaction_tbl t join VType_tbl v on t.vtid = v.vtid join dbo.Location_tbl l on t.locid=l.Locid where dtime between '''+ #date1 +''' and '''+ #date2 +'''
and Status = 5) d pivot ( count(Vtype) for Vtype in (' + #cols + ')) p '
exec sys.sp_executesql #query
end
but I while executing this am getting error like this:
Column 'Location_tbl.LocName' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Related
I am getting the about error while trying to run the following SQL. I am trying to run a dynamic query with several different parameters. Any advice or suggestions will be greatly appreciated.
/* ----- For Testing ----- */
DECLARE #StartDate NVARCHAR(10) = '04/01/2018',
#EndDate NVARCHAR(10) = '04/01/2018',
#Location NVARCHAR(MAX) = 'Newcastle, Salt Creek',
#DayType NVARCHAR(20) = 'Calendar',
#SQL NVARCHAR(MAX)
SELECT #SQL = 'SELECT S.SiteCode,
S.Name AS Site_Name,
L.Name,
DATEADD(HOUR, (T.Period) + 1, P.DateData) AS PeriodEnding,
SUM(T.Ins) AS SumOfIns,
SUM(T.Outs) AS SumOfOuts
FROM dbo.Location L
INNER JOIN dbo.Traffic T ON L.ID = T.LocationID
INNER JOIN dbo.Publish P ON T.PublishID = P.ID
INNER JOIN dbo.Site S ON P.SiteID = S.ID
WHERE ((P.DateData) BETWEEN '+ #StartDate +' AND '+ #EndDate +')
AND T.Ins > -1
AND ((T.EditSequence) = 0)
AND ((P.CanPublish) = 1)
AND L.IsActive = 1
AND (L.IsInternal = 0 OR L.IsInternal IS NULL)
AND S.CustomerID = 1
AND S.Name in (''' + REPLACE(#Location,',',''',''') + ''')
AND ('+ #DayType +' = ''Calendar'')
OR ('+ #Location +' IN (LT.Name) AND '+ #DayType +' = ''Business'' AND (T.Period between 7 and 17))
GROUP BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)
ORDER BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)';
EXEC sp_executesql #sql
Thanks again for all of the help. My code now looks like this...
DECLARE #StartDate NVARCHAR(10) = '04/01/2018',
#EndDate NVARCHAR(10) = '04/01/2018',
#Location NVARCHAR(MAX) = 'Salt Creek,Madill',
#DayType NVARCHAR(12) = 'Calendar',
#SQL NVARCHAR(MAX)
SELECT #SQL = '
SELECT S.SiteCode,
S.Name AS Site_Name,
L.Name,
DATEADD(HOUR, (T.Period) + 1, P.DateData) AS PeriodEnding,
SUM(T.Ins) AS SumOfIns,
SUM(T.Outs) AS SumOfOuts
FROM dbo.Location L
INNER JOIN dbo.Traffic T ON L.ID = T.LocationID
INNER JOIN dbo.Publish P ON T.PublishID = P.ID
INNER JOIN dbo.Site S ON P.SiteID = S.ID
WHERE ((P.DateData) BETWEEN '''+ CONVERT(NVARCHAR, #StartDate, 109) +''' AND '''+ CONVERT(NVARCHAR, #EndDate, 109) +''')
AND T.Ins > -1
AND ((T.EditSequence) = 0)
AND ((P.CanPublish) = 1)
AND L.IsActive = 1
AND (L.IsInternal = 0 OR L.IsInternal IS NULL)
AND S.CustomerID = 1
AND LTRIM(S.Name) in (''' + REPLACE(#Location,',',''',''') + ''')
AND '+ #DayType +' = ''Calendar''
OR (S.Name in (''' + REPLACE(#Location,',',''',''') + ''') AND '+ #DayType +' = ''Business'' AND T.Period between 7 and 17)
GROUP BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)
ORDER BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)';
print #sql
CREATE TABLE #DailyTracking (Site_Code INT, Site_Name VARCHAR(50), Location_Name VARCHAR(50), PeriodEnding DATE, SumOfIns INT, SumOfOuts INT)
INSERT INTO #DailyTracking EXEC sp_executesql #sql
SELECT * FROM #DailyTracking
DROP TABLE #DailyTracking
I am getting the error - Msg 207, Level 16, State 1, Line 20 Invalid column name 'Calendar'.
I am trying to use this stored procedure to create a report in SSRS. The problem is I am trying to pass the #DayType parameter to run the query between 2 separate time frames. If I declare the parameter within the dynamic SQL it will work but I don't know how to pass the parameter from the SSRS report to within the #sql dynamic sql. Any tips/advice? All of the help has been great.
BETWEEN '+ #StartDate +' AND '+ #EndDate +')
should be
BETWEEN '''+ #StartDate +''' AND '''+ #EndDate +''')
Your code should be like below
DECLARE #StartDate NVARCHAR(10) = '04/01/2018',
#EndDate NVARCHAR(10) = '04/01/2018',
#Location NVARCHAR(MAX) = 'Newcastle, Salt Creek',
#DayType NVARCHAR(20) = 'Calendar',
#SQL NVARCHAR(MAX)
SELECT #SQL = 'SELECT S.SiteCode,
S.Name AS Site_Name,
L.Name,
DATEADD(HOUR, (T.Period) + 1, P.DateData) AS PeriodEnding,
SUM(T.Ins) AS SumOfIns,
SUM(T.Outs) AS SumOfOuts
FROM dbo.Location L
INNER JOIN dbo.Traffic T ON L.ID = T.LocationID
INNER JOIN dbo.Publish P ON T.PublishID = P.ID
INNER JOIN dbo.Site S ON P.SiteID = S.ID
WHERE ((P.DateData) BETWEEN '''+ #StartDate +''' AND '''+ #EndDate +''')
AND T.Ins > -1
AND ((T.EditSequence) = 0)
AND ((P.CanPublish) = 1)
AND L.IsActive = 1
AND (L.IsInternal = 0 OR L.IsInternal IS NULL)
AND S.CustomerID = 1
AND S.Name in (''' + REPLACE(#Location,',',''',''') + ''')
AND ('+ #DayType +' = ''Calendar'')
OR (S.Name in (''' + REPLACE(#Location,',',''',''') + ''') AND '+ #DayType +' = ''Business'' AND (T.Period between 7 and 17))
GROUP BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)
ORDER BY S.SiteCode,
S.Name,
L.Name,
DATEADD(HOUR, (T.Period)+1, P.DateData)';
print #sql
--EXEC sp_executesql #sql
I have store procedure like this:
ALTER procedure [dbo].[ParkingSummary1] #startdate varchar(100), #enddate varchar(100) as begin
declare #date1 datetime = CONVERT(datetime, #startdate + ' 00:01:00.000', 120);
declare #date2 datetime = CONVERT(datetime, #enddate + ' 23:23:59.000', 120);
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT LocName, ' + #cols + '
from
( select l.LocName,Vtype from Transaction_tbl t join VType_tbl v on t.vtid = v.vtid
join dbo.Location_tbl l on t.locid=l.Locid where dtime between '''+ #date1+''' and '''+#date2+''' and Status = 5 ) d
pivot
(
count(Vtype)
for Vtype in (' + #cols + ')
) p ' exec sys.sp_executesql #query
end
.while am passing startand enddate like this:
#startdate = '2013-08-05',#enddate = '2013-08-08'
am getting error like this:Conversion failed when converting date and/or time from character string.
what is wrong with my stored procedure ? instead of Date1 and date2 if i pass startdate and enddate then it will work.but that time if i given same date then not coming any result
I would use SET DATEFORMAT YMD as the first line of your stored procedure.
ALTER procedure [dbo].[ParkingSummary1] #startdate varchar(100), #enddate varchar(100)
AS
BEGIN
SET DATEFORMAT DMY
declare #date1 datetime = CONVERT(datetime, #startdate + ' 00:01:00.000', 120);
declare #date2 datetime = CONVERT(datetime, #enddate + ' 23:23:59.000', 120);
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set #query = 'SELECT LocName, ' + #cols + '
from
( select l.LocName,Vtype from Transaction_tbl t join VType_tbl v on t.vtid = v.vtid
join dbo.Location_tbl l on t.locid=l.Locid where dtime between '''+ #date1+''' and '''+#date2+''' and Status = 5 ) d
pivot
(
count(Vtype)
for Vtype in (' + #cols + ')
) p ' exec sys.sp_executesql #query
END
I had to replace
declare #date1 datetime = CONVERT(datetime, #startdate + ' 00:01:00.000', 120);
declare #date2 datetime = CONVERT(datetime, #enddate + ' 23:23:59.000', 120);
by
declare #date1 nvarchar(100) = convert(varchar, #startdate+' 00:00:00.000', 120)
declare #date2 nvarchar(100) = convert(varchar, #enddate+' 23:59:59.000', 120)
I have a stored procedure like this:
ALTER PROCEDURE [dbo].[ParkingDeatailsReportcheck]
#locid INTEGER ,
#startdate NVARCHAR(100) ,
#enddate NVARCHAR(100)
AS
BEGIN
DECLARE #cols AS NVARCHAR(MAX) ,
#query AS NVARCHAR(MAX)
SELECT #cols = STUFF((SELECT DISTINCT
',' + QUOTENAME(Vtype)
FROM VType_tbl
FOR XML PATH('') ,
TYPE
).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET #query = 'SELECT LocName,Date, ' + #cols
+ ' from ( select l.LocName, v.Vtype, convert(date, dtime) as Date from Transaction_tbl t inner join VType_tbl v on t.vtid = v.vtid
join Location_tbl l on t.locid=l.Locid where locid='
+ CAST(#locid AS VARCHAR(MAX)) + ' and dtime between '''
+ #startdate + ''' and ''' + #enddate + ''' ) d pivot (
count(Vtype)
for Vtype in (' + #cols + ') ) p '
EXECUTE(#query)
END
while executing this stored procedure getting error like this:Ambiguous column name 'locid'.
my expected output like this:
locationname Date Emaar Staff Lost Ticket Normal VIP VVIP
---------- ----------- ----------- ----------- ----------- -----------
Fashion 2013-05-08 1 0 2 0 1
Fashion 2013-05-25 0 0 1 1 0
Fashion 2013-05-27 0 0 17 2 1
You have
join Location_tbl l on t.locid=l.Locid where locid='
change to
join Location_tbl l on t.locid=l.Locid where l.locid='
you need the table alias, it's in the dynamic SQL after the where.
Try to change:
where locid='
to:
where t.locid='
Full query:
ALTER PROCEDURE [dbo].[ParkingDeatailsReportcheck]
#locid INTEGER,
#startdate NVARCHAR(100),
#enddate NVARCHAR(100)
AS
BEGIN
DECLARE #cols AS NVARCHAR(MAX)
,#query AS NVARCHAR(MAX)
SELECT
#cols = STUFF((
SELECT DISTINCT ',' + QUOTENAME(Vtype)
FROM VType_tbl
FOR XML PATH (''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET #query = '
SELECT LocName,Date, ' + #cols + '
from (
select l.LocName, v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t
inner join VType_tbl v on t.vtid = v.vtid
join Location_tbl l on t.locid=l.Locid
where t.locid=' + CAST(#locid AS VARCHAR(MAX)) + '
and dtime between ''' + #startdate + ''' and ''' + #enddate + '''
) d
pivot (
count(Vtype) for Vtype in (' + #cols + ')
) p '
EXECUTE (#query)
END
I have a stored procedure like this:
ALTER procedure [dbo].[ParkingDeatailsReportnew]
#locid INTEGER, #startdate nvarchar(100),#enddate nvarchar(100)
as
begin
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype) from VType_tbl FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set #query = 'SELECT Date, ' + #cols + ' from ( select v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t inner join VType_tbl v on t.vtid = v.vtid where dtime between ''' + #startdate + ''' and ''' + #enddate + '''and locid = ' + (select l.Locid from Location_tbl l)
+ ' ) d pivot ( count(Vtype) for Vtype in (' + #cols + ') ) p '
execute(#query)
end
while executing this query I get following error:
expects parameter '#locid', which was not supplied.i want to take all locid from my location table
You Stored Procedure failing to run since you it is expecting get parameters #locid, #StartDate, and #EndDate
EXEC ParkingDeatailsReportnew
#logid = [yourvalue], #StartDate = '[somedate]', #enddate = 'someEndDate'
for example:
EXEC ParkingDeatailsReportnew
#logid = 1234, #StartDate = '2013/08/04 08:01:00 ', #enddate = '2013/08/04 11:21:00'
after you solve this, start solving other issues
--- edit ----
so you need to remove it from the parameters.
ALTER procedure [dbo].[ParkingDeatailsReportnew]
#startdate nvarchar(100),#enddate nvarchar(100)
Why did you have it in the first place?
I doubt that it worked at all execute requires VARCHAR, you have use sp_executesql when you use NVARCHAR
I also think that you have other errors in your logic, but you'll get there later.
Try this one -
ALTER PROCEDURE [dbo].[ParkingDeatailsReportnew]
#locid INTEGER,
#startdate NVARCHAR(100),
#enddate NVARCHAR(100)
AS BEGIN
DECLARE
#cols AS NVARCHAR(MAX)
, #query AS NVARCHAR(MAX)
SELECT #cols = STUFF((
SELECT DISTINCT ',' + QUOTENAME(Vtype)
FROM dbo.VType_tbl
FOR XML PATH(''), TYPE).value('.', 'NVARCHAR(MAX)'), 1, 1, '')
SET #query = '
SELECT Date, ' + #cols + '
from (
select v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t
join VType_tbl v on t.vtid = v.vtid
where dtime between ''' + #startdate + '''
and ''' + #enddate + '''
and locid = ' + CAST(#locid AS VARCHAR(10)) +
' ) d pivot ( count(Vtype) for Vtype in (' + #cols + ') ) p '
EXEC sys.sp_executesql #query
END
alter procedure [dbo].[ParkingDeatailsReport]
#locid INTEGER, #startdate nvarchar(100),#enddate nvarchar(100)
as
begin
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
select #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl FOR XML PATH(''),
TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
set #query = 'SELECT Date, ' + #cols + '
from ( select v.Vtype, convert(date, dtime) as Date
from Transaction_tbl t inner join VType_tbl v
on t.vtid = v.vtid
where dtime between #startdate and #enddate
and locid = ' + CAST(#locid as varchar(max))
+ ' ) d pivot ( count(Vtype) for Vtype in (' + #cols + ') ) p '
execute(#query)
end
I am trying to execute like this:
exec ParkingDeatailsReport 5, '2013-01-01 00:00:00','2013-06-18 23:59:59'
but, I'm getting an error:Must declare the scalar variable "#startdate".
The problem is that you are building SQL in the proc, but you are not using the values in #startdate and #enddate, instead you are passing the string
You need to grab the values of these variables when you build the string - something like:
ALTER PROCEDURE [dbo].[ParkingDeatailsReport]
#locid INTEGER,
#startdate nvarchar(100),
#enddate nvarchar(100)
as
BEGIN
DECLARE #cols AS NVARCHAR(MAX),
#query AS NVARCHAR(MAX)
SELECT #cols = STUFF((SELECT distinct ',' + QUOTENAME(Vtype)
from VType_tbl FOR XML PATH(''),
TYPE).value('.', 'NVARCHAR(MAX)') ,1,1,'')
SET #query = 'SELECT Date, ' + #cols + '
FROM (
SELECT
v.Vtype,
convert(date, dtime) as Date
FROM Transaction_tbl t
INNER JOIN VType_tbl v
ON t.vtid = v.vtid
WHERE
dtime between ''' + #startdate + ''' and ''' + #enddate + '''
AND locid = ' + CAST(#locid as varchar(max)) + '
) d
PIVOT ( count(Vtype)
FOR Vtype in (' + #cols + ') ) p '
EXECUTE(#query)
END