How to write UPDATE SQL with Table alias in SQL Server 2008? - sql

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';

Related

Problem with Stored Procedures in PHPmyAdmin

Attempting to perform operations with a random integer in SQL.
The following code works perfectly as intended when run as pure SQL, but triggers a syntax error when attempting to save it into a stored procedure.
SET #sample_count = (SELECT count(*)
FROM cinder_sample);
SELECT #sample_count;
SET #random_ID = (SELECT FLOOR(RAND()*#sample_count));
SELECT #random_ID;
Any ideas as to what could be going wrong?
The exact error triggered is:
"The following query has failed: "CREATE DEFINER=root#localhost PROCEDURE play_random_sp() NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER DELIMITER // SET #sample_count = (SELECT count() FROM cinder_sample)// SELECT #sample_count// SET #random_ID = (SELECT FLOOR(RAND()#sample_count))// SELECT #random_ID"
MySQL said: #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '// SET #sample_count = (SELECT count(*) FROM cinder_sample)// SELECT' at line 1"

Why does this UPDATE statement fail in H2?

This UPDATE statement works when run against Postgres, but fails when run against H2. What's the equivalent H2 statement?
UPDATE award_details
SET award_id = a.id
FROM awards a
WHERE a.award_details_id = award_details.id;
The H2 error message is:
SQL State : 42000
Error Code : 42000
Message : Syntax error in SQL statement "​​[*]
UPDATE AWARD_DETAILS
SET AWARD_ID = A.ID
FROM AWARDS A
WHERE A.AWARD_DETAILS_ID = AWARD_DETAILS.ID"; SQL statement:
Your current update syntax looks to be Postgres style. Instead use correlated subquery update syntax:
UPDATE award_details aw
SET award_id = (SELECT a.id FROM awards a WHERE a.award_details_id = aw.id);

sql 2000 is'not executing the query that 2008 can execute

If I execute the following SQL statement in SQL server 2008 it works perfectly, but when execute the same statement in SQL Server 2000 it doesn't work:
Statement:
select top 1
[k].[FixbiUnitPrice]
from (
select top (select COUNT(*)
from [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f)
[FixbiUnitPrice],
BDate,
biNumber
From [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f
where f.BAccCustID != 0x0
order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]
Output in SQL Server 2000:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FixbiUnitPrice'.
What's wrong with syntax for SQL Server 2000
Support for 'dynamic' top in sql server started in 2005 version.
for sql server 2000 you can only use a constant number after top.
you can probably use SET ROWCOUNT for your query.
Also, Read this post and it's answers.
In addition to #Zohar's point that a variable TOP #N isn't supported in Sql 2000, what you could also do is generate Dynamic Sql and then execute it, i.e.:
DECLARE #TopCount INT
DECLARE #Sql NVARCHAR(2000)
SELECT #TopCount = COUNT(*)
FROM [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817')
SET #Sql =
'select top 1
[k].[FixbiUnitPrice]
from (
select top ' + CONVERT(NVARCHAR(50), #TopCount) + '
[FixbiUnitPrice],
BDate,
biNumber
From [dbo].[mnrFnBI_Fixed](''4E591E71-33BD-4ECC-8703-771BE8A76817'') f
where f.BAccCustID != 0x0
order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]'
EXEC sp_executesql #Sql
That said, Sql 2000 is really an unsupported, legacy technology and you need to consider upgrading to a more recent version of Sql Server as soon as possible.

Oracle SQL update from one Table to another table throws syntax error

Oracle SQL update from one Table to another table throws syntax error for following simple update query.
UPDATE Sales_Import SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID;
Error:
Error starting at line 1 in command:
Error at Command Line:2 Column:37
Error report:
SQL Error: ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"
I believe the same query will work in other databases like postgres etc.
Could any one tell the correct query ?
and whatever i tried is this ANSI standard query ?
and whatever i tried is this ANSI standard query ?
No. Oracle Oracle doesn't support join in update statement.
In Oracle you could do it in two ways -
**Merge statement **
with only update clause in merge statement
MERGE INTO sales_import s
USING (SELECT *
FROM retrieveaccountnumber) u
ON (u.leadid = s.leadid)
WHEN matched THEN
UPDATE SET u.accountnumber = s.accountnumber;
Correlated query
UPDATE sales_import t1
SET accountnumber = (SELECT t2.accountnumber
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid )
WHERE EXISTS (
SELECT 1
FROM retrieveaccountnumber t2
WHERE t1.leadid = t2.leadid );
I will write your sql like this:
UPDATE Sales_Import SI
SET AccountNumber = (Select RAN.AccountNumber
FROM RetrieveAccountNumber RAN
WHERE RAN.LeadID = SI.LeadID);
You can do this by joining those tables:
UPDATE SI
SET AccountNumber = RAN.AccountNumber
FROM RetrieveAccountNumber RAN
JOIN Sales_Import SI ON RAN.LeadID = SI.LeadID;

Error in Update T-SQL in SQL Server 2005

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