March 21, 2013 at 2:09 pm
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?
March 21, 2013 at 2:17 pm
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 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/
March 21, 2013 at 2:29 pm
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
March 21, 2013 at 3:06 pm
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/
March 21, 2013 at 9:02 pm
So you are recommending to do the check in the proc itself, rather than relying on the table definition to drive the logic?
March 22, 2013 at 7:28 am
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 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/
March 22, 2013 at 9:00 am
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?
March 22, 2013 at 10:25 am
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.
March 22, 2013 at 10:32 am
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 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/
March 22, 2013 at 10:46 am
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.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply