1

I have a few table's in my DB.

collection of db tables

I am trying to update the rota table using a SQL stored procedure. I want the user to be able to type in, for example;

Name: Adam Brookes

Shift: Middle Day

On Call: Secondary

and using a subquery have it look up the id's of those and place them into the rota table. At the moment I am testing in Postman where I can define the 'date' and 'datekey' manually.

My Stored Procedure currently looks like this;

ALTER PROCEDURE [dbo].[RotaUpdateItems]
    @date_key int,
    @date date,
    @user_id varchar(max),
    @shift_type_id int,
    @type_call_id int

AS
BEGIN

    SET NOCOUNT ON;

    UPDATE rota
    SET date = @date,
        user_id = (
        SELECT user_id 
        FROM staff s
        WHERE s.first_name LIKE 'ABC%'
        ),
        shift_type_id = (
        SELECT shift_type_id 
        FROM shifts h
        WHERE h.shift_type LIKE 'ABC%'
        ),
        type_call_id = (
        SELECT type_call_id 
        FROM on_call c 
        WHERE c.type_of_call LIKE 'ABC%')
    WHERE date_key = @date_key
END

I have referenced this question to get the LIKE 'ABC%' code to see if that would help convert "Primary" into '1', but this did not fully answer my question

I also researched this answer from John to see if a join is necessary when running an update query but this did not fully answer my question.

6
  • Why are you looking up the type_call_id inside a sub-query? You already have a parameter @type_call_id with an integer value in it? Commented Feb 12, 2018 at 10:47
  • I did this because I thought I want to select the type_call_id from the table ''on_call'', where I check the ''type_of_call'' against what the user has input (Secondary) Commented Feb 12, 2018 at 10:55
  • So, update your question to reflect what you're actually trying to do. As I understand it you want the parameters to be VARCHAR(32) or similar? And then the sub-queries to have c.type_of_call = @type_of_call, for example? Commented Feb 12, 2018 at 11:01
  • Sorry I thought I made it clear with the paragraph but it's not clear enough. I have trouble wording these things. I have changed the parameters from int to varchar and tried your example but the test still puts back ''Validation failed for parameter 'shift_type_id'. Invalid Number Commented Feb 12, 2018 at 11:16
  • So what is the question? Commented Feb 12, 2018 at 11:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.