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
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 have a situation which prevent me of updating rows in a table in MSSQL getting the data from ORACLE. I can INSERT fine from ORACLE to MS SQL using a SELECT statement like:
SELECT XRECORDACTIVATIONDATE, XRECORDCUTOFFDATE, XRECORDREVIEWDATE,
XRECORDFILINGDATE, XNOLATESTREVISIONDATE, XNEWREVISIONDATE, XDATERECEIVEDDOC,
XINACTIVEDATE, DCREATEDATE, DINDATE, DRELEASEDATE, DLASTMODIFIEDDATE
FROM STELLENT.V_EXPORT_TO_MSSQL V
But when I try to update the rows based on an unique ID using:
UPDATE D
SET D.XRECORDACTIVATIONDATE = V.XRECORDACTIVATIONDATE
FROM DBO.DOCUMENT D
INNER JOIN STELLENT."V_EXPORT_TO_MSSQL" V ON D.DID = V.DID
I get the following error:
ORA-00933: SQL command not properly ended
(System.Data.OracleClient)
DBO.DOCUMENT is a MSSQL table.
STELLENT.V_EXPORT_TO_MSSQL is a View in ORACLE
I might be writing wrong the query I will appreciate some help. thank you.
Lukasz is correct - a select statement is a lot different from an insert statement.
The ORA-00933 error means your query is not formed properly. This is because in Oracle, the database expects queries to follow a certain format/standard. Typically, queries within Oracle will have a form of SELECT [columns] FROM [tables] WHERE [conditions]. This can vary - for example if you wanted to select all data from a table, that query might look like "SELECT * FROM [table];" and the WHERE clause can be omitted because you do not need to define a condition for the database to return all rows. While queries can vary in form, in general, they will follow some type of format.
You are receiving this error because your query does not conform to the expected form, and it is because you have an INNER JOIN that directly follows your FROM clause. To fix this, I would recommend creating a query that you use to select the records you want to update, and then using that select statement to form your update statement by replacing the "SELECT" with "UPDATE".
For more on SQL Standards and how to format your queries, I would recommend taking a look at Oracle documentation. https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_1001.htm#SQLRF52344
When I write a sql statement containing an in clause, if the column in the in statement does not exist it does not throw an error, but just ignores the in statement and returns a resultset.
Query below will return all rows from T1
SELECT * FROM T1 WHERE Id IN(SELECT Id FROM T2)
Is that the correct behaviour for SQL Azure?
My local MS SQL server throws an error saying the column does not exist.
Frankly, what you are describing is impossible. If the column inside the IN statement doesn't exist, Azure will throw an exception. A result set cannot possibly be returned. Currently there is very little difference between the way SQL Server executes a query and the way Azure v12 does. Each may generate a different execution plan, but essentially there is very little difference between the two.
This is expected behavior, same as in Sql Server. Basically, if a column is referenced in a subquery that does not exist in the table referenced by the subquery's FROM clause, but exists in a table referenced by the outer query's FROM clause, the query executes without error. SQL Server implicitly qualifies the column in the subquery with the table name in the outer query.
This is documented here:
https://technet.microsoft.com/en-us/library/ms178050(v=sql.105).aspx
I am trying to execute a very simple SQL query using "Microsoft Query". I can connect to the tables, select the columns I need but when I try to execute I get error msg saying "Incorrect Syntax near keyword 'IS'"
I got the SQL statement below through automated query but it just doesn't execute successfully. I think I know what the issue is. It is because my database catalog name is "IS". I tried executing same query on my other databases with different names and it works fine. Since I have access to several databases I need to specifiy which db I am accessing in my script and that's when it causes this issue. Is there a work around in my situation where I can avoid using database name and perhaps declare a variable?
SELECT Table1.id,
Table1.Name,
Table1.Status,
Table1.DateEntered
FROM IS.dbo.Table1 Table1
OR
SELECT * FROM IS.dbo.Table1 Table1 (Same error msg)
IS is a SQL reserved keyword, you have to wrap it with []
SELECT * FROM [IS].dbo.Table1 Table1 (Same error msg)
however, is a good practice - and error avoiding technique - to name tables without using reserved keywords or to always use brackets around tables name
I'd assume IS is a reserved keyword. Try wrapping it around square brackets:
SELECT Table1.id, Table1.Name, Table1.Status, Table1.DateEntered FROM [IS].dbo.Table1 Table1
I have a inline select statement to calculate the product of the set of values.
Since SQL Server 2005 doesn't have a built in Product aggregate function, I am using LOG/EXP to get it.
My select statement is:
(select exp(sum(log(value))) from table where value > 0)
Unfortunately I keep getting the following error:
Msg 3623, Level 16, State 1, Line 1
A domain error occurred.
I've ensured that none of the values are zero or negative so I'm not really sure why this error is occurring. Does anyone have any ideas?
One of the features of the query planner introduced in SQL 2005 is that, in some circumstances where the table statistics indicate it will be more efficient, the WHERE clause of a statement will be processed after the SELECT clause.
(I can't find the Books On-Line reference for this right now).
I suspect this is what is happening here. You either need to exclude the rows where value = 0 before carrying out the calculation - the most reliable way being to store the rows you need in a temporary (#) table - or to modify your query to handle zero internally:
SELECT EXP(SUM(LOG(ISNULL(NULLIF(VALUE,0),1)))) AS result
FROM [table]
The NULLIF\ISNULL pair I have added to your query substitutes 1 for 0 - I think this will work, but you will need to test it on your data.