I want to update some data in EditEmail table from DBUsers database to EditEmail table in DBCurrent datebase.So i got error when i execute following statement:
USE DBCurrent
UPDATE [DBUsers].[dbo].[EditEmail] EN
SET EN.MailSubject = E.MailSubject,
EN.MailMessage = E.MailMessage
FROM
(
SELECT * FROM EditEmail
) AS E
WHERE EN.Type = E.Type
Error message:
ERROR:
Msg 102, Level 15, State 1, Line 2
Incorrect syntax near 'EN'.
Msg 156, Level 15, State 1, Line 8
Incorrect syntax near the keyword 'AS'.
Is there any syntax mistake in this T-SQL?
You cannot give the table you're specifying in the UPDATE statement a table alias (for whatever reason - don't ask me why this isn't possible, ask the T-SQL design team)...
Try this statement instead:
USE DBCurrent
UPDATE
[DBUsers].[dbo].[EditEmail]
SET
MailSubject = E.MailSubject,
MailMessage = E.MailMessage
FROM
dbo.EditEmail E
WHERE
DBUsers.dbo.EditEmail.Type = E.Type
AND E.Type = 'blahblah' -- or whatever additional conditions you have!
You need to specify the table being updated in full, e.g. with its database, schema, table and column name, in the WHERE clause.
There is also no need for your "artificial" subquery there to reference the EditMail table - just define that in the FROM clause and give it a table alias (here, they're supported).
T-SQL doesn't allow to define an alias on the table updated,
but it allows to update an alias:
This fixes the syntax, I think the logic must still be improved.
Please try (I didn't check):
UPDATE EN
SET EN.MailSubject = E.MailSubject,
EN.MailMessage = E.MailMessage
FROM [DBUsers].[dbo].[EditEmail] EN
join EditEmail E
on EN.Type = E.Type
Related
I have a query as the below one:
alter table [toolDB].[dbo].[esn_sho] add esn_umts_sho_relation_key as Convert(nvarchar(50),[utrancell])+'_'+Convert(nvarchar(50),[utranrelation])
So All I need I want to replace esn_umts_sho_relation_key column if exist...
As I got this error:
[Microsoft][SQL Server Native Client 11.0][SQL Server]Column names in each table must be unique. Column name 'esn_umts_sho_relation_key' in table 'toolDB.dbo.esn_sho' is specified more than once
I tired to use the below code but it's doen't work:
IF NOT EXISTS (alter table [toolDB].[dbo].[esn_sho] add esn_umts_sho_relation_key as Convert(nvarchar(50),[utrancell])+'_'+Convert(nvarchar(50),[utranrelation]))
It gives me this error:
Incorrect syntax near the keyword 'alter'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'
So I want to add If Exist in this query any one knows how to solve this prolem...
The column I already exist in the table ,but I want to replace it if exist to escape from this error...
IF COL_LENGTH('TableName', 'ColumnName') IS NOT NULL
BEGIN
-- Column Exists
-- here you can put your cond...
END
IF COL_LENGTH('[toolDB].[dbo].[esn_sho]', 'esn_umts_sho_relation_key') IS NOT NULL
BEGIN
-- Column Exists
alter table [toolDB].[dbo].[esn_sho] add esn_umts_sho_relation_key as Convert(nvarchar(50),[utrancell])+'_'+Convert(nvarchar(50),[utranrelation])---Put your condition in proper way...
END
IF EXISTS(SELECT * FROM DatabaseName where ColumnName = #YourParameter)
BEGIN
You Can Write Alter Query Here
END
ELSE
BEGIN
You Can Set Here AN Else Condition/Optional
END
To solve this particular issue, you can use the system tables. Try this
SELECT *
FROM sys.all_objects obj
JOIN sys.all_columns col ON obj.object_id = col.object_id
WHERE
obj.Name = #YourTableName
AND col.Name = #YourColumnName
This will give you the columns in your table if it exists. You can use this to make your decision about what you would do if it exists/doesn't exist.
I'm getting a syntax error :
Msg 156, Level 15, State 1, Procedure trgDiscoverSurchargeChangeTiming, Line 26
Incorrect syntax near the keyword 'select'
on a trigger I'm writing. Now, I normally avoid triggers because I tend to forget them later when I am making changes to the DB, so I'm a little rusty on them, but I feel like this one should be fine:
(TRIGGER STUFF)Begin
if update(surchargepay)
begin
insert into
dbo.CustomErrorLog
(errorText
, ErrorOrderID
, errorOldValue
, errorNewValue)
values
select -- This is where the error is being thrown
convert(varchar(50), getdate())
, i.routeid
, d.surchargepay
, i.surchargepay
from
inserted i INNER JOIN
deleted d on i.id = d.id
end
End
any ideas what might cause that?
You don't need the "value" keyword when using "select" to populate an insert.
I have a table I want to update this with a view in SQL Server 2008
When I write update sql code like this :
UPDATE [dorsadbfitupdetail].[dbo].[tbl_wl_Joint]
SET
[JntLineNoInternalUse] = dbo.IpmiLineInternal.LnNo
WHERE (dbo.tbl_wl_Joint.JntLineNoInternalUse IS NULL)
GO
SQL Server throws an error:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "dbo.IpmiLineInternal.LnNo" could not be bound.
What can I do to resolve it?
Try this one -
UPDATE j
SET JntLineNoInternalUse = i.LnNo
FROM dbo.tbl_wl_Joint j
JOIN dbo.IpmiLineInternal i ON j.ID = i.ID /* simple change your id columns */
WHERE j.JntLineNoInternalUse IS NULL
I have a very basic UPDATE SQL -
UPDATE HOLD_TABLE Q SET Q.TITLE = 'TEST' WHERE Q.ID = 101;
This query runs fine in Oracle, Derby, MySQL - but it fails in SQL server 2008
with following error:
"Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Q'."
If I remove all occurrences of the alias, "Q" from SQL then it works.
But I need to use the alias.
The syntax for using an alias in an update statement on SQL Server is as follows:
UPDATE Q
SET Q.TITLE = 'TEST'
FROM HOLD_TABLE Q
WHERE Q.ID = 101;
The alias should not be necessary here though.
You can always take the CTE, (Common Tabular Expression), approach.
;WITH updateCTE AS
(
SELECT ID, TITLE
FROM HOLD_TABLE
WHERE ID = 101
)
UPDATE updateCTE
SET TITLE = 'TEST';
I've written a stored procedure as following:
CREATE PROC spSoNguoiThan
#SNT int
AS
begin
IF not exists (select column_name from INFORMATION_SCHEMA.columns where
table_name = 'NhanVien' and column_name = 'SoNguoiThan')
ALTER TABLE NhanVien ADD SoNguoiThan int
else
begin
UPDATE NhanVien
SET NhanVien.SoNguoiThan = (SELECT Count(MaNguoiThan)FROM NguoiThan
WHERE MaNV=NhanVien.MaNV
GROUP BY NhanVien.MaNV)
end
SELECT *
FROM NhanVien
WHERE SoNguoiThan>#SNT
end
GO
Then I get the error :
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 12
Invalid column name 'SoNguoiThan'.
Server: Msg 207, Level 16, State 1, Procedure spSoNguoiThan, Line 15
Invalid column name 'SoNguoiThan'.
Who can help me?
Thanks!
When the stored proc is parsed during CREATE the column does not exist so you get an error.
Running the internal code line by line works because they are separate. The 2nd batch (UPDATE) runs because the column exists.
The only way around this would be to use dynamic SQL for the update and select so it's not parsed until EXECUTE time (not CREATE time like now).
However, this is something I really would not do: DDL and DML in the same bit of code
I ran into this same issue and found that in addition to using dynamic sql I could solve it by cross joining to a temp table that had only one row. That caused the script compiler to not try to resolve the renamed column at compile time. Below is an example of what I did to solve the issue without using dynamic SQL
select '1' as SomeText into #dummytable
update q set q.ValueTXT = convert(varchar(255), q.ValueTXTTMP) from [dbo].[SomeImportantTable] q cross join #dummytable p