question

newklear avatar image
newklear asked

Convert Select statement into Update statement

Hello Everyone I have a **Select Statement**, how could I convert this to an **Update Statement** please where I only want to update the "per.[PERSONNAME]" with a new name. Please see below for the Update Statement: SELECT DISTINCT ref.[REQUISITIONID] as 'Requisition ID', CONVERT(VARCHAR(128), DATEADD(HOUR,2,ref.[SUBMITTEDDATETIME]),20) as 'Date Submitted', ser.[SERVICENAME] as 'Service Name', ini.[PERSONNAME] as 'Initiator', ini.[LOGINNAME] as 'Initiator LoginID', cus.[PERSONNAME] as 'Customer', cus.[LOGINNAME] as 'Customer LoginID', dtf.[TASKNAME] as 'Task Name', dtf.[TASKSTATUS] as 'Task Status', CONVERT(VARCHAR(128), DATEADD(HOUR,2,dtf.[STARTEDDATETIME]),20) as 'Task Started Date', CONVERT(VARCHAR(128), DATEADD(HOUR,2,dtf.[COMPLETEDDATETIME]),20) as 'Task Completed Date', per.[PERSONNAME] as 'Service Owner', DATEDIFF(day, dtf.STARTEDDATETIME,dtf.COMPLETEDDATETIME) as 'Duration in Days' FROM [Datamart].[dbo].[DM_REQUISITIONENTRYFACT] ref INNER JOIN [Datamart].[dbo].[DM_AUTHTASKFACT] dtf ON ref.[REQUISITIONID] = dtf.[REQUISITIONID] INNER JOIN [Datamart].[dbo].[V_DM_SERVICE] ser ON ref.[SERVICEID] = ser.[SERVICEID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] ini ON ref.[REQUESTORID] = ini.[PERSONID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] cus ON ref.[CUSTOMERID] = cus.[PERSONID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] per ON dtf.[PERFORMERID] = per.[PERSONID] WHERE dtf.[STARTEDDATETIME] BETWEEN '2016-08-01' AND '2016-09-21' AND ser.[SERVICENAME] = 'Lync' AND ref.[SERVICEREQUESTSTATUS] = 'Completed' AND (dtf.[TASKNAME] LIKE 'Service Owner Approval%' OR dtf.[TASKNAME] LIKE 'SO Approval%') ORDER BY CONVERT(VARCHAR(128), DATEADD(HOUR,2,dtf.[STARTEDDATETIME]),20),CONVERT(VARCHAR(128), DATEADD(HOUR,2,dtf.[COMPLETEDDATETIME]),20),[Duration in Days]
updateselectjoins
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
Kev Riley avatar image
Kev Riley answered
Take everything from the `FROM` clause down to the bottom (ignoring the order by) as this defines your target data set, and update that e.g.: UPDATE per SET per.[PERSONNAME] = 'New Name' FROM [Datamart].[dbo].[DM_REQUISITIONENTRYFACT] ref INNER JOIN [Datamart].[dbo].[DM_AUTHTASKFACT] dtf ON ref.[REQUISITIONID] = dtf.[REQUISITIONID] INNER JOIN [Datamart].[dbo].[V_DM_SERVICE] ser ON ref.[SERVICEID] = ser.[SERVICEID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] ini ON ref.[REQUESTORID] = ini.[PERSONID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] cus ON ref.[CUSTOMERID] = cus.[PERSONID] INNER JOIN [Datamart].[dbo].[V_DM_PERSON] per ON dtf.[PERFORMERID] = per.[PERSONID] WHERE dtf.[STARTEDDATETIME] BETWEEN '2016-08-01' AND '2016-09-21' AND ser.[SERVICENAME] = 'Lync' AND ref.[SERVICEREQUESTSTATUS] = 'Completed' AND (dtf.[TASKNAME] LIKE 'Service Owner Approval%' OR dtf.[TASKNAME] LIKE 'SO Approval%') I'd suggest running this in a `BEGIN TRAN....ROLLBACK` and also run the original query to see if it gives you the desired result, both in terms of data and expected number of rows touched - and once it does run with `COMMIT`
4 comments
10 |1200 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Hello Kev Thank You so much for your well explained response, really appreciate. Helped me gain a little understanding. I have run your query provided and left with the error message: "Update or insert of view or function 'per' failed because it contains a derived or constant field.". With reading now, I see I might need to add dummy data as I am entering a single insert on multi-table views. If this is the case, how could I work around this please?
0 Likes 0 ·
Sorry hadn't realised that those are views - you need to restate the query using the base tables (for PER at least), but it feels like the query is to complex for what you need - after all if you update the name for the person that this query returns, and assuming a normalized data design, then that 'person' is updated regardless of the dtf.STARTDATETTIME and ser.SERVICENAME and ref.SERVICEREQUESTSTATUS etc. Is this a one-off update? Maybe better to retrieve the PK of the person and run a simple update from that manually?
0 Likes 0 ·
Hello Kev Yes this is a once off update. Using just: `update per. SET per.[PERSONNAME]='Jack Black' where ser.SERVICENAME= 'Lync'; ` I receive the error: `Incorrect syntax near the keyword 'SET'. `
0 Likes 0 ·
instead of updating per, which is a view you need to update the underlying table - whatever that is!
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.