Creating a temp table and using aggregate select query - sql

I am trying to create a temp table from a sub select query and am receiving an error message
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ';'
Here is my query:
select *
into #temp
from
(select dollars, PostingDate, EntryDescription
from MillsEntOps.dbo.OE_InvoiceGLSumm
where Postingdate between '2018/03/01' and '2018/04/11')
The error message is :
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near ')'.
Any ideas what I am doing wrong?

Alternatively you can write:
select dollars, PostingDate, EntryDescription
into #TEMP
from MillsEntOps.dbo.OE_InvoiceGLSumm
where Postingdate between '2018/03/01' and '2018/04/11'

I think you missed alias name
select * into #temp from ( select dollars, PostingDate, EntryDescription from MillsEntOps.dbo.OE_InvoiceGLSumm where Postingdate between '2018/03/01' and '2018/04/11')temp_table /* alias name temp_table */

Related

T-SQL selecting CTE into a variable

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.

Is it possible to add SQL alias from subquery?

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)

SQL Union select get max length from result

I have query against a SQL Server database:
Select [Surname]
from [dbo].[customer]
union
Select 'Surname';
As result I have:
and I want get from result max(len( {result} ));. If I use:
Select max(len([Surname]))
from [dbo].[customer];
This is working correctly (in result I have 11), but I need add to compare column name.
Query:
Select max(len(Select [Surname]
from [dbo].[customer]
union
Select 'Surname'))
Return error:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near ')'.
Try changing it to
Select max(len([Nazwisko]))
FROM (Select [Nazwisko] from [dbo].[wlasciciel] union Select 'Surname') t

Pivot table not working properly

I have following query:
select * from dbPratiche
pivot
(
count(Compagnia)
for
(convert(char(3), [Data creazione pratica], 0))
in ([jan],[feb],[mar],[apr],[may],[jun],[jul],[aug],[sep],[October],[nov],[dec])
) pvt
I want to pivot my table on monthname from one [Data creazione pratica] column having date values in it.
But I'm getting an error:
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '('.
I checked all brackets, all brackets are correct.
Please tell me where I am making a mistake in this query
I solved it through:
select * from (
select convert(char(3), [Data creazione pratica], 0) as monthOF ,[Compagnia]
from dbPratiche where ISNULL([Data creazione pratica],'')!='')as temp
pivot
(
count(Compagnia)
for monthOF in
(jan,feb,mar,apr,may,jun,jul,aug,sep,Oct,nov,dec)
) pvt

Msg 102, Level 15, State 1, Line 7 Incorrect syntax near ')'

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'