I get these errors:
Msg 156, Level 15, State 1, Line 22
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 22
Incorrect syntax near ')'
when running this code:
Select *
from
(select
DWF_Week_No
,DWF_Fuel_Name
,DWF_T_Sale
from
QF_FDashboard_Week_Fuel
where
DWF_Month_Name = 'October'
and DWF_Location_Name = 'Fitzroy') t
pivot
(sum(DWF_T_Sale)
for DWF_Fuel_Name in (select distinct DWF_Fuel_Name from QF_FDashboard_Week_Fuel)
) as pivot_table;
It is not possible to put a SELECT inside the IN in the PIVOT. You can find a workaround here. In your case it would be something like this:
DECLARE #col nvarchar(max) = (stuff( ( select distinct ',[' + Ltrim(rtrim(DWF_Fuel_Name)) +']' from QF_FDashboard_Week_Fuel FOR XML PATH('')),1,1,''))
EXEC('Select *
from
(select
DWF_Week_No
,DWF_Fuel_Name
,DWF_T_Sale
from
QF_FDashboard_Week_Fuel
where
DWF_Month_Name = ''October''
and DWF_Location_Name = ''Fitzroy'') t
pivot
(sum(DWF_T_Sale)
for DWF_Fuel_Name in ('+#col+')
) as pivot_table;
')
Related
I am attempting to set a variable to the result of a CTE select statement in SQL Server / T-SQL:
DECLARE #ReportRecipients VARCHAR(MAX);
SET #ReportRecipients = WITH CTE_TableName AS (SELECT a.emailTExt FROM
CSLEventAUP_Edit a JOIN CSLEventAUP_EventEditJunction b ON (a.Id = b.EditId)
JOIN CSLEventAUP_Events c ON (b.EventId = c.Id)
WHERE c.LogicalDeleteIn = 0
AND c.StaffEvent = 1
AND a.emailText IS NOT NULL
UNION
SELECT emailText FROM CSLEventAUP_Edit WHERE CyberGroup = 1)
SELECT TOP 1 STUFF((
SELECT ';' + emailTExt
FROM CTE_TableName t1
ORDER BY t1.emailText
FOR XML PATH('')),1, Len(';'), '') AS EmailTexts
FROM CTE_Tablename t0;
I get these errors:
Msg 156, Level 15, State 1, Procedure EmailSignAUPReminder, Line 38
[Batch Start Line 9]
Incorrect syntax near the keyword 'With'.
Msg 319, Level 15, State 1, Procedure EmailSignAUPReminder, Line 38 [Batch Start Line 9]
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
Running the query by itself results in
bob.xxx#mail.mil;bryan.xxx.xx#xx.mil;bryan.xx.xx#xx.edu;fred.coordinator#xx.mil
You need to define your CTE and SELECT its results in two separate steps. As this is structured, you are attempting to SELECT from an expression that does not yet exist.
I am inserting a job step in Azure Elastic job agent. This is my code:
EXEC jobs.sp_add_jobstep #job_name='Sample T-SQL',
#command=N' declare #columns varchar(max)
select #columns = stuff(( select '],[' + [$Name]
from dbo.groups
order by '],[' + convert(varchar(max), [$_ClosingBalance]) desc
for xml path('')), 1, 2, '' + ']'
INSERT INTO dbo.Trial1
( [Bank Accounts],[Bank OD A/c],[Branch / Divisions],[Capital Account],[Cash-in-Hand],[Current Assets],[Current Liabilities],[Deposits (Asset)]
,[Direct Expenses],[Direct Incomes],[Duties & Taxes],[Fixed Assets],[Duties & Taxes1],[Duties & Taxes2],[Indirect Expenses],[Indirect Incomes],[Investments]
,[Loans & Advances (Asset)],[Loans (Liability)],[Misc# Expenses (ASSET)],[Provisions],[Purchase Accounts],[Reserves & Surplus],[Sales Accounts],[Secured Loans]
,[Stock-in-Hand],[Stock Transfer Outward],[Sundry Creditors],[Sundry Debtors],[Suspense A/c],[Duties & Taxes3]
,[Unsecured Loans]
)
Select * from (
Select [$_ClosingBalance], RowN = Row_Number() over (order by #columns) from dbo.Groups
) a
pivot (max([$_ClosingBalance]) for RowN in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31],[32])) p
INSERT INTO dbo.Date ([Date]) VALUES (GETDATE())
INSERT INTO dbo.Final
SELECT *
FROM dbo.Trial1
INNER JOIN dbo.Date
ON dbo.Trial1.IdCol = dbo.Date.IDCol1'',
#credential_name='JobRun',
#target_group_name='DatabaseGroup5'
I am receiving the following error :
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ']'.
Msg 137, Level 15, State 2, Line 15
Must declare the scalar variable "#columns".
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near ''
My code is ending where it should not. Please help.
after that you assigned #command ,you can write : print #command
then you can look printed script ,copy it and paste in query window.then probebly you will understand your mistake.
When I use below 2 queries separately I get output successfully
Query #1
DECLARE #ID VARCHAR(1000)
SET #ID = ''
SELECT #ID = #ID + CAST(int_ID AS VARCHAR(1000))
FROM MATRIX_RELATIONSHIP
SELECT COUNT(int_ID) [Count], YEAR(dt_5841_OF) [date]
FROM TRANS_IDENTIFICATIONS
WHERE smallint_209_ME = 4
AND CHARINDEX(CONVERT(VARCHAR, int_ID), ('''' + Replace((#ID),',',''',''') + '''')) > 0
GROUP BY YEAR(dt_5841_OF);
Output:
Count | year
02 2016
Query #2
SELECT
COUNT(*) [Count1], YEAR(dt_modifiedOn) [date]
FROM
MATRIX_RELATIONSHIP
GROUP BY
YEAR(dt_modifiedOn);
Output:
Count | year
02 2016
But when I use both queries to form a full outer join something like below:
SELECT
tab1.Count, tab2.Count1, tab1.date
FROM
(DECLARE #ID VARCHAR(1000)
SET #ID = ''
SELECT #ID = #ID+CAST(int_ID AS VARCHAR(1000))
FROM MATRIX_RELATIONSHIP
SELECT COUNT(int_ID) [Count], YEAR(dt_5841_OF) [date]
FROM TRANS_IDENTIFICATIONS
WHERE smallint_209_ME = 4
AND CHARINDEX(convert(varchar,int_ID), ('''' + Replace((#ID),',',''',''') + '''')) > 0
GROUP BY YEAR(dt_5841_OF)) tab1
FULL OUTER JOIN
(SELECT COUNT(*) [Count1], YEAR(dt_modifiedOn) [date]
FROM MATRIX_RELATIONSHIP
GROUP BY YEAR(dt_modifiedOn)) tab2 ON tab1.date = tab2.date;
I get these errors:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'DECLARE'.
Msg 102, Level 15, State 1, Line 4
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 7
Incorrect syntax near 'tab2'.
Expected result
Count | Count1 | year
02 02 2016
EDIT
I have tried placing declare and set outside the query too but still there the issue persists
DECLARE #ID VARCHAR(1000) SET #ID=''
SELECT tab1.Count,tab2.Count1,tab1.date from
( SELECT #ID= #ID+CAST(int_ID AS VARCHAR(1000))
from MATRIX_RELATIONSHIP SELECT COUNT(int_ID)[Count],YEAR(dt_5841_OF)[date] FROM TRANS_IDENTIFICATIONS WHERE smallint_209_ME=4 and CHARINDEX(convert(varchar,int_ID), ('''' + Replace((#ID),',',''',''') + '''')) >
0 GROUP BY YEAR(dt_5841_OF)) tab1
FULL OUTER JOIN
(SELECT COUNT(*)[Count1],YEAR(dt_modifiedOn)[date]
FROM MATRIX_RELATIONSHIP GROUP BY YEAR(dt_modifiedOn)) tab2
ON tab1.date=tab2.date;
Which leads to another set of errors:
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near '='.
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Msg 102, Level 15, State 1, Line 8
Incorrect syntax near 'tab2'.
DECLARE #ID VARCHAR(1000)
SET #ID='';
SELECT #ID = #ID + CAST(int_ID AS VARCHAR(1000)) from MATRIX_RELATIONSHIP;
SELECT MAX(Count) AS Count, MAX(Count1) AS Count1, date
FROM
(
SELECT COUNT(int_ID)[Count], null AS [Count1], YEAR(dt_5841_OF)[date]
FROM TRANS_IDENTIFICATIONS
WHERE smallint_209_ME = 4
and CHARINDEX(convert(varchar,int_ID), ('''' + Replace((#ID),',',''',''') + '''')) >
0 GROUP BY YEAR(dt_5841_OF)
UNION ALL
SELECT NULL AS [Count], COUNT(*)[Count1],YEAR(dt_modifiedOn)[date]
FROM MATRIX_RELATIONSHIP
GROUP BY YEAR(dt_modifiedOn)
)AS temp
GROUP BY [date]
this is not a valid from
it is two select statements
from
( DECLARE #ID VARCHAR(1000)
SET #ID='' SELECT #ID= #ID+CAST(int_ID AS VARCHAR(1000))
from MATRIX_RELATIONSHIP
SELECT COUNT(int_ID)[Count],YEAR(dt_5841_OF)[date]
FROM TRANS_IDENTIFICATIONS
WHERE smallint_209_ME=4
and CHARINDEX(convert(varchar,int_ID), ('''' + Replace((#ID),',',''',''') + '''')) > 0
GROUP BY YEAR(dt_5841_OF)
)
Your DECLARE and SET has to be outside of the SELECT. But what is its purpose?
remove the DECLARE #ID VARCHAR(1000);
SET #ID='' from the select statement.
Also declare the variable at the top of your statement
and rather writing two separate select statement use inner join on int_id and then do the outer join it will work
DECLARE #ID VARCHAR(1000)
SET #ID = ''
select * from
(
SELECT #ID+ CAST(MATRIX_RELATIONSHIP.int_ID AS varchar(1000)) AS count1, COUNT(TRANS_IDENTIFICATIONS.int_ID) AS count2, YEAR(TRANS_IDENTIFICATIONS.dt_5841_OF) AS date1
FROM MATRIX_RELATIONSHIP INNER JOIN
TRANS_IDENTIFICATIONS ON MATRIX_RELATIONSHIP.int_ID = TRANS_IDENTIFICATIONS.int_ID
WHERE (TRANS_IDENTIFICATIONS.smallint_209_ME = 4) AND (CHARINDEX(CONVERT(varchar, TRANS_IDENTIFICATIONS.int_ID), '''' + REPLACE(#ID, ',', ''',''') + '''') > 0)
GROUP BY YEAR(TRANS_IDENTIFICATIONS.dt_5841_OF),MATRIX_RELATIONSHIP.int_ID
union
(
SELECT '' as count1, COUNT(*) [Count2], YEAR(dt_modifiedOn) [date1]
FROM MATRIX_RELATIONSHIP
GROUP BY YEAR(dt_modifiedOn)
)
) as tbl
I hope this works for you... Do let me know
I want to add aliases to sql query from select subquery.
something like
SELECT
ID AS(
SELECT
TOP1 NAME
FROM MYTABLE
)
,NAME
,SURNAME
FROM PEOPLE
Is it possible?
Error:
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '('. Msg 102,
Level 15, State 1, Line 1 Incorrect syntax near ','. Msg 156, Level
15, State 1, Line 8 Incorrect syntax near the keyword 'and'
Only possible with this:
DECLARE #n VARCHAR(MAX), #sql VARCHAR(MAX)
SELECT TOP 1 #n = NAME FROM MYTABLE
SET #sql = 'SELECT ID AS ' + #n + ', NAME, SURNAME FROM PEOPLE'
EXEC(#sql)
Using this query:
select *
from
( Select DISTINCT status
From MNPdata
where IMSI_no = 'abc'
)
I got:
Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'
Give the subquery an alias:
select *
from (
Select DISTINCT status From MNPdata where IMSI_no = 'abc'
) AS t
Since the subquery selects only status, you can do this instead:
Select DISTINCT status From MNPdata where IMSI_no = 'abc'