Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Should I rely on the table definition to implement logic in a stored proc? Expand / Collapse
Author
Message
Posted Thursday, March 21, 2013 2:09 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 25, Visits: 2,472
I want to hear your thoughts on this. Is there a correct way to do this or accepted best practice? Here is the scenario.

CREATE TABLE dbo.parent(some_id TINYINT IDENTITY(1, 1), string_val VARCHAR(10), CONSTRAINT pk PRIMARY KEY(some_id))
CREATE TABLE dbo.child(some_blah SMALLINT, some_id_from_parent TINYINT NOT NULL, CONSTRAINT f FOREIGN KEY (some_id_from_parent) REFERENCES dbo.parent(some_id)

CREATE PROC dbo.insert_into_child(@list_of_string_vals)

-- @list of string_vals converted to a table var
BEGIN TRY
BEGIN TRAN
INSERT INTO dbo.child(some_id_from_parent)
SELECT P.some_id
FROM dbo.parent P
LEFT JOIN @list_of_string_vals C ON P.string_val = C.string_val -- left join, so if there are nulls then insert will fail cos of column definition
COMMIT TRAN
END TRY
BEGIN CATCH
-- roll back
END CATCH

Lets say I have a table called dbo.child with a integer column (some_id_from_parent) that is defined as NOT NULL. The values in this column would be foreign key children from a parent table and each primary key in the parent table has a string value. The app would pass in a list of these string values, and I do a (left) join on the parent table to gather the id values and insert them in the child table. Now in the list of string values that are passed, there might be a value that does not belong in the original parent table; and in such a case, none of the string values that are passed down should be recorded in the child table.

So in my proc, I put this insert in a transaction and I am relying on the table definition where the column is declared as not null. So when a string value that is not in the parent table comes in as part of a list, then the id value for that would be null (since i am doing left join) and the insert would fail because the column is declared as not null; so rollback the transaction.

However, I am wondering if this is a good way of implementing this. Main concern being, if someone changes the table definition for that column to be nullable, then this proc no longer functions as defined. Should I explicity check for null values in the proc and then make a decision to rollback (which seems to be a more robust way of implementing this, but looks like more explicit work)?

In general, how should I treat the table definitions? Are they representing strictly business rules that tell what kind of data goes into the table or can I use that information to implement other logic?
Post #1434019
Posted Thursday, March 21, 2013 2:17 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:59 PM
Points: 13,007, Visits: 12,426
I don't understand why you doing the left join? You say something about making sure the values are ok. That is the point of the foreign key, so you don't have to do this manually. Why not just take your list, run it through a string splitter (like the one in my signature) and you are good to go.

_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1434023
Posted Thursday, March 21, 2013 2:29 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 25, Visits: 2,472
Hi Sean,

Prehaps I am not being clear. Kindly excuse my explanation skills.

The reason I am doing a left join is this. Lets say the parent table has string_vals 'AAA', 'BBB', 'CCC' with a ids 1, 2, 3 respectively. Now lets say the app passes in the values 'AAA', 'BBB'; in this case I gladly insert the values 1 and 2 (for 'AAA' and 'BBB' respectively) in the child table because both 'AAA' and 'BBB' are in the parent table. Now lets say the app passes in the values 'AAA', 'BBB' and 'DDD'. Since 'DDD' is not present in the parent table, I shouldnt be inserting anything into the child table. So when I do a left join, there will be a null value and I am relying on that NULL value to drive the logic.

(Incidentally, I am in fact using the splitter in your signature to convert the string_vals I get into a table.)

Does this
Post #1434026
Posted Thursday, March 21, 2013 3:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:59 PM
Points: 13,007, Visits: 12,426
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1434043
Posted Thursday, March 21, 2013 9:02 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 25, Visits: 2,472
So you are recommending to do the check in the proc itself, rather than relying on the table definition to drive the logic?
Post #1434117
Posted Friday, March 22, 2013 7:28 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:59 PM
Points: 13,007, Visits: 12,426
Sam S Kolli (3/21/2013)
So you are recommending to do the check in the proc itself, rather than relying on the table definition to drive the logic?


No you should ignore my previous post because I was having a senior moment.

Keeping it far simpler this will do the same thing and just lets your constraints do their thing.

create PROC [dbo].[insert_into_child](@list_of_string_vals varchar(8000))
AS
BEGIN TRY
BEGIN TRAN
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 'inside catch'
rollback transaction
END CATCH
GO




_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1434264
Posted Friday, March 22, 2013 9:00 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:58 PM
Points: 20,705, Visits: 32,356
Quick question, A, B, C are in the parent table; app passes in A, B, D do you still want to insert A, B and ignore D since A and B are in the parent table and D isn't or do you not want to insert A, B, and D since D isn't in the parent table even though A and B are?



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1434317
Posted Friday, March 22, 2013 10:25 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 25, Visits: 2,472
Lynn...since D is not present in the parent table, none of A, B, D should be inserted even though A and B are present in the parent table.

The overarching question I have is if I can rely on table definition to drive my logic in the stored proc. I can see the pros and cons, but I wanted to see how other people's experiences are like.
Post #1434379
Posted Friday, March 22, 2013 10:32 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 3:59 PM
Points: 13,007, Visits: 12,426
Sam S Kolli (3/22/2013)
Lynn...since D is not present in the parent table, none of A, B, D should be inserted even though A and B are present in the parent table.

The overarching question I have is if I can rely on table definition to drive my logic in the stored proc. I can see the pros and cons, but I wanted to see how other people's experiences are like.


It is generally a better idea to use the constraints to control this type of thing. That is what they are designed to do. They will perform better in most cases than roll your own sql. And of course in your case you have the constraints so they will be checked even if you do it in code too. Then becomes the challenges of what happens if the business rules change? If you use constraints to control RI (which is the best approach) all you have to change is your constraints. If you use code you have to change your code in all locations AND your constraints.


_______________________________________________________________

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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1434386
Posted Friday, March 22, 2013 10:46 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Yesterday @ 9:09 PM
Points: 25, Visits: 2,472
Thank You Sean....I have currently implemented it by relying on the constraints. For now, we have decided to go with this as it seemed more elegant.
Post #1434394
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse