Home Forums SQL Server 2008 SQL Server 2008 - General Should I rely on the table definition to implement logic in a stored proc? RE: Should I rely on the table definition to implement logic in a stored proc?

  • OK I think I get what you were saying. I was just too dense to get it. I think something like this should get you started.

    CREATE PROC dbo.insert_into_child(@list_of_string_vals varchar(8000))

    AS

    BEGIN TRY

    BEGIN TRAN

    if exists

    (

    SELECT P.some_id

    FROM dbo.DelimitedSplit8K(@list_of_string_vals, ',') C

    LEFT JOIN dbo.parent P ON P.string_val = C.Item

    where p.string_val is null

    )

    RAISERROR('Not all values are in parent table.', 11, 1) --Must have a severity of 11 or higher and less than 20 so we get to the catch block

    INSERT INTO dbo.child(some_id_from_parent)

    SELECT P.some_id

    FROM dbo.DelimitedSplit8K(@list_of_string_vals, ',') C

    LEFT JOIN dbo.parent P ON P.string_val = C.Item

    COMMIT TRAN

    END TRY

    BEGIN CATCH

    select 'Do something here'

    rollback transaction

    END CATCH

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/