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

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

Related

What kind of T-SQL table hint should I use in the SELECT before an INSERT statement? [duplicate]

This question already has answers here:
Upsert with concurrency checking SQL Server
(1 answer)
Insert if not exists avoiding race condition
(2 answers)
Closed 7 months ago.
I have a SQL query as below, running within REPEATABLE READ transaction.
IF NOT EXISTS (SELECT * FROM TableName WHERE Column1Name = #Column1Value)
INSERT INTO TableName VALUES (#Column1Value, #Column2Value);
The above query gets run within multiple processes. I see a possibility when process 1 does a SELECT and then process 2 also does as SELECT (as there is shared lock) before either of the processes get chance to do an INSERT, there would be a deadlock. I would like to avoid this. What is the correct table hint to use for this situation? Note that it is not possible to enable READ_COMMITTED_SNAPSHOT on the database.
I would not use a hint but instead do this in one operation. Check out Davide's article and try and apply his method with the single merge statement. In my opinion the preferred method.

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.

Using an Alias to restart SQL sequence with [duplicate]

This question already has an answer here:
How to set 'start with' of sequence to select query result in SQL server?
(1 answer)
Closed 5 years ago.
I'd like to make it so, that a sequence starts with the max value(+1) of a certain column. This is what I've got so far:
SELECT MAX(customer_number)+1 AS HighestCustomerNumberPlusOne FROM organisation;
CREATE SEQUENCE customer_number_sequence
START WITH 1
INCREMENT BY 1;
ALTER SEQUENCE customer_number_sequence RESTART WITH HighestCustomerNumberPlusOne;
And I get the error:
Error: Incorrect syntax near 'HighestCustomerNumberPlusOne'.
SQLState: S0001
ErrorCode: 102
What is the correct syntax? I'm using SQL Server 2012 but would prefer just generic SQL if it's possible, since the T-SQL is harder than learning Chinese.
ALTER SEQUENCE syntax accepts only constants in the RESTART WITH clause.
So, you'll have to save the result of your query into a variable; construct a string with ALTER SEQUENCE SQL command and embed the value of the variable in the SQL text, then execute it as dynamic SQL using EXEC or sp_executesql.
But, normally there is no need to reset the sequence.
What do you really need?

SQL Server : Adding Column Error [duplicate]

This question already has answers here:
SQL Query to add a new column after an existing column in SQL Server 2005
(7 answers)
Closed 7 years ago.
I am still finding my feet in SQL Server, and I am trying to do a simple column addition and it is throwing an error. I am trying to add the KinAdr3 column after the KinAdr2 column but it is throwing the following error
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near 'AFTER'.
Below is the SQL statement I am trying to execute
ALTER TABLE TBL_MEDICAL_Patient
ADD KinAdr3 nVARCHAR(50)
AFTER KinAdr2
Thanks in advance
The reason is simple as the above syntax is valid in MySQL but is not valid in SQL Server. In SQL Server following syntax is valid:
ALTER TABLE tablename ADD columnname INT
However, a user wanted to add the column between two of the columns. SQL Server is relational engine. The order of the column should not matter in any of the T-SQL operations. It does not matter in most of the cases (except when the table is extra large, and it has many NULL columns it impacts the size of the table). In reality whenever user wants to add a column to the table, he/she should just the column and later retrieve the column in a specific order in the table using column names.
It is always a good idea to specify the name of the column in the T-SQL query (using * is indeed a bad idea but that is out of scope of this blog post).
For more details please see the SQL SERVER – How to Add Column at Specific Location in Table

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.

Resources