Running Common Table expression in netezza as a script with Update at the end of CTE [duplicate] - netezza

This question already has an answer here:
Convert a Recursive CTE in SQL Server to netezza
(1 answer)
Closed 6 years ago.
I am trying to run a CTE in script form
e.g
with CTE as
(select * from sometable);
on running this code part alone in the I am getting the error
^ found "" (at char 152) expecting SELECT' or'('' */
Please let me know how to run this query. thanks

You are getting this error because you aren't doing anything with the CTE you have defined, but the syntax requires something.
Add a SELECT after the CTE clause in the parentheses and it should work fine.
with CTE as
(select * from sometable)
select * from CTE;

Related

STRING_AGG replacement in SQL Server 2016 [duplicate]

This question already has answers here:
Query to get multiple row into single row
(3 answers)
how to stuff date and time in SQL
(2 answers)
ListAGG in SQLSERVER
(4 answers)
Closed 3 years ago.
I would like to aggregate single column values with an separator in between and with some prefix.
Below is the code tried which works.
SELECT concat('TNB/IAG/',STRING_AGG(WF_ValStr, '/')) AS Result
FROM wfattrdata where wf_id=35262472 and WF_AttrID in (28,29,30,31,33);
I could get the below expected result in latest MSSql versions.
TNB/IAG/1/2/3/4/5
How to replace the above query to make it work in SQL Server 2016?
Check the below STUFF,XML code -
SELECT DISTINCT CONCAT('TNB/IAG/',T1.results) AS EXPECTED_RESULT FROM
(
select REPLACE(STUFF(CAST((
SELECT ' /' +CAST(c.WF_ValStr AS VARCHAR(MAX))
FROM (
SELECT distinct WF_ValStr
FROM wfattrdata
) c
FOR XML PATH(''), TYPE) AS VARCHAR(MAX)), 1, 2, ''),' ','') AS results
from wfattrdata t) T1;

Query not running in SQL Server [duplicate]

This question already has answers here:
How do I 'subtract' sql tables?
(14 answers)
Closed 4 years ago.
I have this select statement that I am running in SQL Server. But it's throwing an error:
select count(*)
from
(select zip from A
minus
select zip from B)
Error:
Incorrect syntax near select
What is the issue here? I have also tried aliasing the subquery but same error happens.
There is nothing called minus in SQL Server, you need to use except.
Note, except in SQL Server is equivalent to minus of Oracle
Following query will work.
select count(*) ct
from
(
select zip from A
except
select zip from B
)t
Another issue with your code is that you need to give a alias name to the inner table you are creating.

Passing variable in Group By in SQL Server [duplicate]

This question already has answers here:
I need to pass column names using variable in select statement in Store Procedure but i cannot use dynamic query
(3 answers)
Where can parameters be used in SQL statements?
(2 answers)
Using SQL parameters to DROP INDEX in C#
(2 answers)
Closed 5 years ago.
How can I pass variable in Group BY expression.Here is my code.
and from #Varientsku I want to pass column name
Create Proc spIsUnique
#varientsku nvarchar(max) output
As
Begin
select #varientsku,
IIf (count(*)>1,'Dublicate','True') as Total from product group by
#varientsku
End.
and I get this error.
Each GROUP BY expression must contain at least one column that is not an outer reference.
Help me to solve this
You need dynamic sql
EXEC ('select '+#varientsku+',
IIf (count(*)>1,''Duplicate'',''True'') as Total
from product
group by '+#varientsku)
Note : Make sure #varientsku is sanitized, this code is vulnerable to sql injection

Syntax Error with my SQL Create Table As [duplicate]

This question already has answers here:
How to create a table from select query result in SQL Server 2008 [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to create a new table in Microsoft SQL Server Management Studio based on two existing tables.
When I execute the query below, I get an error saying that there is an:
Incorrect syntax near the keyword 'SELECT'.
SQL code:
CREATE TABLE NEW_TABLE AS
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
I looked at various other problems, but could not find a solution to mine. Do you have any idea what could be wrong with the syntax?
Alternatively, you can use SELECT * INTO new_table statement just like this.
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C INTO NEW_TABLE
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
this statement will also create a new table as you required.

SQL Server deleted records even though there was an error in the subquery which located in where clause with IN tag [duplicate]

This question already has answers here:
sql server 2008 management studio not checking the syntax of my query
(2 answers)
Closed 8 years ago.
As you see from these images, the ROSHEADERID column is invalid and when I execute the first one it returns a meaningful message. But when I use this query in where clause as a subquery. It executes and deletes all of the records without warning or aborting the operation.
How can this be ?
The subquery has access to columns in outer query, thus column ROSHEADERID you ask for is effectively taken from EXM_REVIEWOFSYSTEMS (not from EXM_REVIEWOFSYSTEMSHEADER), thus deleting all records in outer table.
This should clarify a bit on what's going on behind the scenes:
http://sqlfiddle.com/#!2/623c7/3
More information here: http://technet.microsoft.com/en-us/library/ms187638(v=sql.105).aspx
Use Alias Names for tables to avoid conflict:
BEGIN TRAN
DELETE FROM EXM_REVIEWOFSYSTEMS
WHERE ROSHEADERID IN ( SELECT rsh.ROSHEADERID
FROM EXM_REVIEWOFSYSTEMSHEADER rsh
WHERE rsh.PATIENTID = '' )
ROLLBACK TRAN

Resources