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
Related
I need some help with some SQL Server 2014 syntax.
I want to use the NEWID() function to create the UUID value in column CareplannersUUID. Each time a record is inserted, I want a new UUID value to appear in column CareplannersUUID.
But I am not sure about the syntax of the command. I think it is something like:
ALTER TABLE CareplannersMembers
ADD CONSTRAINT DF_table_id DEFAULT (NEWID()) FOR CareplannersUUID
I am wondering specifically, is there another value I should add for DF_table_id?
Notes about the table in question:
Table name: CareplannersMembers
UUID column name: CareplannersUUID
Thank you for your help.
Eric
I looked at the similar questions, but am not able to determine, from information presented there, whether or not I am using the correct syntax for my SQL Server statement.
I am trying to create a trigger using linked database to my Oracle Database. If I do select statement, it works that means my Linked Server is working perfectly but when I try to create trigger with it i get this error:
The object name 'TESTS..TESTSMS.YELL_CAT' contains more than the
maximum number of prefixes. The maximum is 2.
See my query below.
CREATE TRIGGER Insert_Into_TempYellCat ON TESTS..TESTSMS.YELL_CAT
after INSERT AS
BEGIN
INSERT INTO
TempYellCat
(
TRA_DATE,
TRA_SEQ1,
TRA_SEQ2
)
SELECT
TRA_DATE,
TRA_SEQ1,
TRA_SEQ2
FROM
TESTS..TESTSMS.YELL_CAT
END
Please I need someone to assist.
This is a dupe question. Here is the solution on SO 2 years ago.
The object name contains more than the maximum number of prefixes. The maximum is 3
Specify your schema name SQL05.ManufacturingPortal.dbo.[OPC.WriteRequests] and object name with square brackets
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?
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
I am trying to execute stored proc through SSIS and it gives me following error:
[Execute SQL Task] Error: Executing
the query "Exec sp1 ?" failed with
the following error: "Procedure: sp1
Line: 66 Message: Cannot insert
duplicate key row in object
'table.sq1' with unique index
'UIX_sp1_Key'.". Possible failure
reasons: Problems with the query,
"ResultSet" property not set
correctly, parameters not set
correctly, or connection not
established correctly.
Actually the stored Proc sp1 is truncating & reloading a data into a table.
I am not able to figure out where exactly its trying to insert duplicate record.
Thanks in advance.
Your data must have duplicate values across the key column(s). You could remove the primary key temporarily (or create another table with the same structure but without the definition of the primary key, but that would mean changing the stored procedure), then load the data into the table. Then use this SQL statement:
select keycol1 {,keycol2 {,keycol3} ...}, count(*)
from tablename
group by keycol1 {,keycol2 {,keycol3} ...}
having count(*) > 1
That will show you the data values that are duplicates across the key column(s).
If you are truncating the table before load, then you must have duplicate data in the source.
Examine this to see what there is. use Excel or Access or some such if needed to assist. Or drop the unique constraint then query the staging table with an aggregate query.