Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to use a SELECT statement inside a CONCAT?

I’m trying to create a string from table values, along with some text to make it more readable.

The only problem I’m having is that I can’t get the SELECT statement to work inside my CONCAT. It’s been bugging me for quite some time now, and I would appreciate any feedback on what I’m doing wrong or if there is another way to do this.

My SQL script:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CREATE PROCEDURE dbo.spDepartment_UpdateDepartment
    @UserId INT, 
    @Id INT, 
    @Name VARCHAR(128)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO EventLog(Content)
    VALUES(CONCAT('User: ', @UserId, ', has updated a department name from: ', SELECT Name FROM Department WHERE Id = @Id, ' to: ', @Name)
END

>Solution :

Just change it to an insert from a select

INSERT INTO EventLog(Content)
SELECT CONCAT('User: ', @UserId, ', has updated a department name from: ', dpmt.Name, ' to: ', @Name)
FROM Department dpmt WHERE Id = @Id;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading