I am inserting a custom SQL into QS to query my DB; the query contains a CTE and some selection from the same like below:
with cte as
(
.....
)
select *
from cte;
Whenever I run the query in QS, I get a syntax error.
sourceErrorMessage: Incorrect syntax near the keyword 'with'
Any help on the syntax or whether CTEs are supported or not by QS would be helpful.
Thank you.
Related
I'm new to Microsoft SQL Server 2014. I run this SQL code:
SELECT TOP(10) 'DBSG' as seek_entity, *
FROM DBSG..PM00200
and get this result:
Next, I want to find out total line items for that entity with code below.
WITH vw_pm00200_all AS
(
SELECT TOP(10)
'DBSG' as seek_entity, *
FROM
DBSG..PM00200
)
SELECT
seek_entity,
COUNT(*) AS total
FROM
vw_pm00200_all
GROUP BY
1
Sadly, I get this error. I have no idea why it failed.
Msg 164, Level 15, State 1, Line 9
Each GROUP BY expression must contain at least one column that is not an outer reference.
Lastly, please advise is Microsoft SQL Server based on Transact-SQL?
It looks like you are running into this problem here: Each GROUP BY expression must contain at least one column that is not an outer reference
As the answer points out, grouping by a constant literal is pointless as it is the same for all results. Count(*) will return the same result as Count(*) with a GROUP BY.
If this is just test code and you plan on using a CASE statement (with different values) in place of the string literal, you may have better luck.
Yes, T-SQL is Microsoft SQL Server's flavor of SQL.
I am trying to find the original code behind the creation of a table in Snowflake.
I am using this query:
SELECT GET_DDL('table', 'table1');
This is only giving me the original DDL behind the table. I would need the full code (as in the original SQL SELECT statement).
Anyone know what query could get me that?
You can query QUERY_HISTORY and get the SQL statement (and other data) using the following:
// Be sure to use a role with permission to perform the following
SELECT
*
FROM
SNOWFLAKE.ACCOUNT_USAGE.QUERY_HISTORY
WHERE
QUERY_TEXT ILIKE '%create%table%table1%'
ORDER BY END_TIME DESC
LIMIT 20;
I am trying to find out when a table has been changed but only for rows that meet a specific condition. I have tried with this query:
SELECT CHECKSUM_AGG(BINARY_CHECKSUM(LastChanged)) FROM Products WHERE Category = 2 WITH (NOLOCK)
and I get a General SQL Server error. If I execute the query directly, the error is:
Incorect syntax near '('
Is it possible to get the checksum for the rows that meet the condition only?
The error is being caused by an incorrectly placed query hint, not by your use of a CHECKSUM expression.
Though it might help to read up on whether you should really be using NOLOCK at all, try putting WITH (NOLOCK) after your FROM:
SELECT CHECKSUM_AGG(BINARY_CHECKSUM(LastChanged))
FROM Products WITH (NOLOCK)
WHERE Category = 2
I was running this query in MSSQL:
SELECT * FROM (SELECT * FROM ABC)
It gives an error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
This same command runs just fine on a DB2 database. I know this query doesn't make any sense I was just testing functionality.
So, are there certain features, eg., SELECT in FROM clause, that are not supported in MSSQL that are supported in DB2?
You just need to give the subquery an alias like so:
SELECT * FROM (SELECT * FROM ABC) subTable
Which translates to:
SELECT * FROM (SELECT * FROM ABC) as subTable
The AS is optional.
I've just set up SQL Server 2008 SP2 on my workstation to run a local version of a database. The production database is SQL Server 2008 R2. A colleague is running SQL Server 2005 on his workstation.
My DB is throwing an error with the following code, while other instances of the same DB on the other servers run this query with no error.
WITH Posts AS (
SELECT TOP 10 *
FROM TBL_MSG_LATEST (NOLOCK)
WHERE TBL_MSG_LATEST.STATUS = 1
)
SELECT * FROM Posts (NOLOCK)
...throws this error:
Msg 215, Level 16, State 1, Line 6
Parameters supplied for object 'Posts' which is not a function. If the parameters are intended as a table hint, a WITH keyword is required.
Removing the (NOLOCK) after Posts makes my DB happy.
I'm not familiar with SQL Server, so I don't totally understand CTEs but I believe that this NOLOCK might not even be necessary here.
However, we aren't happy about making a change to the codebase just to satisfy my dev environment.
Is there a config difference with my newer DB environment? Is there a valid reason to remove the NOLOCK that I can't decipher from the error message?
From this page: http://msdn.microsoft.com/en-us/library/ms187373.aspx
Omitting the WITH keyword is a deprecated feature and will be removed
in a future version of Microsoft SQL Server.
So this will work.
WITH Posts AS (
SELECT TOP 10 *
FROM TBL_MSG_LATEST WITH (NOLOCK)
WHERE TBL_MSG_LATEST.STATUS = 1
)
SELECT * FROM Posts WITH (NOLOCK)
Don't know if it is a good idea or not or if it has any effect.
This appears to be specific to CTE syntax. Adding the "WITH" won't change how the NOLOCK works; it will just allow it to work.
With NOLOCK hints in CTEs, these both work:
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST NOLOCK
)
SELECT * FROM Posts NOLOCK;
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST WITH (NOLOCK)
)
SELECT * FROM Posts WITH (NOLOCK);
but this fails:
WITH Posts AS (
SELECT ... FROM TBL_MSG_LATEST (NOLOCK)
)
SELECT * FROM Posts (NOLOCK);
These all work outside of a CTE:
SELECT ... FROM TBL_MSG_LATEST NOLOCK;
SELECT ... FROM TBL_MSG_LATEST (NOLOCK);
SELECT ... FROM TBL_MSG_LATEST WITH (NOLOCK);
The correct syntax is "WITH (NOLOCK)".
But ... if you're putting NOLOCK inside the CTE, why are you also putting it on the SELECT?
Note that the results are all the same in Denali CTP3.