Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Should I rely on the table definition to implement logic in a stored proc?


Should I rely on the table definition to implement logic in a stored proc?

Author
Message
Sam S Kolli
Sam S Kolli
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 3486
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?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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)
Sam S Kolli
Sam S Kolli
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 3486
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
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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
         Wink
         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)
Sam S Kolli
Sam S Kolli
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 3486
So you are recommending to do the check in the proc itself, rather than relying on the table definition to drive the logic?
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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)
Lynn Pettis
Lynn Pettis
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24167 Visits: 37929
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?

Cool
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)
Sam S Kolli
Sam S Kolli
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 3486
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.
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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)
Sam S Kolli
Sam S Kolli
SSC Rookie
SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)SSC Rookie (29 reputation)

Group: General Forum Members
Points: 29 Visits: 3486
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search