SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
Search:  
 
 
        
Home       Members    Calendar    Who's On


«««12345»»

Passing Parameters as (almost) 1, 2, and 3 Dimensional Arrays Expand / Collapse
Author
Message
Posted Thursday, October 30, 2008 6:16 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Monday, September 14, 2009 5:22 AM
Points: 437, Visits: 918
rbarryyoung: (p.s. This is "Humor" also.)

Now, that's funny!
Post #594226
Posted Thursday, October 30, 2008 6:46 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:10 PM
Points: 18,136, Visits: 12,157
jlcampos71 (10/29/2008)
Thanks Again Jeff!

I almost Figured that out... but I didn't at all... I was figuring out that I should make in fact, 3 passes, because there is a second constriction in case the key already exists...

But yes, you are right... my SP would take the data from the Split table (@Elements) and insert or update according to your suggestion.

Thanks again!!!

Jorge Luis


You bet, Jorge. Thanks for the feeback.

Also, my recommendation would to see what happens to performance if you convert @Elements to #Elements... ;)


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #594741
Posted Friday, October 31, 2008 8:56 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 22, 2009 8:25 PM
Points: 5, Visits: 20
I'll do that Jeff... but please let me know... where can I found information about it?

As I told you, I'm a very beginner next to you...

Is there anything in the BOL or something you have wrote about it?

Just to let you know, I have implemented your solution already, After having the @elements table populated, I insert all the data into another table defined as @Ubicacion with the structure I need, and then I do Two different update operations (one condition is managed like a WHERE and the other as an AND in the Join condition) and one INSERT to manage all my diferent cases. Let me show you:

--===== Updates the values that already exists and has different antena.
-- Updates the value of previous antena with current antena
UPDATE Dest
SET ubiIdChip = temp.ubiIdChip,
ubiIdAntenaAnt = dest.ubiIdAntena,
ubiIdAntena = temp.ubiIdAntena,
ubiFecha = temp.ubiFecha,
ubiBateria = temp.ubiBateria
FROM Ubicacion Dest
INNER JOIN @Ubicacion temp
ON Dest.ubiIDChip = temp.ubiIdChip
WHERE dest.ubiIdAntena <> temp.ubiIdAntena

--===== Updates the values that already exists and has the same antena.
-- Current and Previous antena remains the same.
UPDATE Dest
SET ubiIdChip = temp.ubiIdChip,
-- ubiIdAntenaAnt = ubiIdAntena,
-- ubiIdAntena = temp.ubiIdAntena,
ubiFecha = temp.ubiFecha,
ubiBateria = temp.ubiBateria
FROM Ubicacion Dest
INNER JOIN @Ubicacion temp
ON Dest.ubiIDChip = temp.ubiIdChip AND Dest.ubiIdAntena = temp.UbiIdAntena


--===== Insert the values that doesn't exists
-- Previous antena set to -1.
INSERT INTO Ubicacion
SELECT temp.*
FROM @Ubicacion temp
LEFT JOIN Ubicacion Dest
ON Dest.ubiIDChip = temp.ubiIdChip
WHERE Dest.ubiIdChip is NULL


Would you mind to review it?

Thanks again!

Jorge Luis
Post #595053
Posted Friday, October 31, 2008 5:29 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:10 PM
Points: 18,136, Visits: 12,157
jlcampos71 (10/31/2008)
I'll do that Jeff... but please let me know... where can I found information about it?


Yeaup... start by looking up "temporary tables [SQL Server]" (without the quotes) in Books Online. Under the subcategory of "ccreating", they have a section that explains a bit about temp tables. Also, see the following URL...

temporary tables [SQL Server]

Even thought it's an SQL Server 2000 URL, it's still pretty much spot on.



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #595341
Posted Friday, October 31, 2008 5:52 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:10 PM
Points: 18,136, Visits: 12,157
Jorge,

I flattered that you've mimiced the format I use for comments. And, they're pretty much what I would expect to see in a code review.

There are, however, a couple of potential problems with the following code...

--===== Insert the values that doesn't exists
-- Previous antena set to -1.
INSERT INTO Ubicacion
SELECT temp.*
FROM @Ubicacion temp
LEFT JOIN Ubicacion Dest
ON Dest.ubiIDChip = temp.ubiIdChip
WHERE Dest.ubiIdChip is NULL

The comments say that the "Previous antena {will be} set to -1", but I don't see that in the code anywhere.

The other problem is that you should probably include a column list in both the Insert and the Select. Some folks call it a "best practice". I won't go that far, but it is something that I make the habit of doing... that way if the import order of columns changes on the temp table, you don't have to worry about data ending up in the wrong columns on the Ubicacion table. It's just one more "safe guard".


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #595343
Posted Tuesday, November 04, 2008 8:15 AM


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, August 20, 2009 6:30 AM
Points: 59, Visits: 237
J (10/30/2008)
rbarryyoung: (p.s. This is "Humor" also.)
Now, that's funny!

Sitting in a cubicle farm, I tried (unsuccessfully) to not laugh out loud.
Thanks -- it made my day. :)


Paul DB
Post #596609
Posted Tuesday, November 04, 2008 9:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 22, 2009 8:25 PM
Points: 5, Visits: 20
Jeff Moden (10/31/2008)
Jorge,

I flattered that you've mimiced the format I use for comments. And, they're pretty much what I would expect to see in a code review.

Yes, I never though comments should be important in SQL and SPs although I do it a lot while programming in VB.Net, but after reading some of your articles, I realized that it is REALLY Important and nothing better than do it the way the master does. So... Thanks for that.


There are, however, a couple of potential problems with the following code...

The comments say that the "Previous antena {will be} set to -1", but I don't see that in the code anywhere.

I missed that part, there is a place where I set to -1 that column, let me show you... just before the code I pasted here there is this other part of code (commented using the Jeff's way of course)

--===== Insert the final result of the split 
-- as a "table" instead of an "EAV" into @Ubicacion
INSERT INTO @Ubicacion (ubiIdChip, ubiIdAntena, ubiIdAntenaAnt, ubiFecha, ubiBateria)
SELECT MAX(CASE WHEN ColNum = 1 THEN ElementValue END) AS ubiIdChip,
MAX(CASE WHEN ColNum = 2 THEN ElementValue END) AS ubiIdAntena, -1,
CAST(CAST(MAX(CASE WHEN ColNum = 3 THEN ElementValue END) AS Float) AS DateTime) AS ubiFecha,
MAX(CASE WHEN ColNum = 4 THEN ElementValue END) AS ubiBateria
FROM @Elements
GROUP BY RowNum

As you can see, third value is always a (-1) for the ubiAntenaAnt column, I was wondering if this was the best way or using maybe as a variable set to this value and using it only in the insert clause, maybe that is the best way, because I would reduce the size of that table... let me read what you think... please.


The other problem is that you should probably include a column list in both the Insert and the Select. Some folks call it a "best practice". I won't go that far, but it is something that I make the habit of doing... that way if the import order of columns changes on the temp table, you don't have to worry about data ending up in the wrong columns on the Ubicacion table. It's just one more "safe guard".


Got it! I think that way too... it was a lazy second I guess!!! Fixed!

Thanks again!

Jorge Luis
Post #596669
Posted Tuesday, November 04, 2008 6:58 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:10 PM
Points: 18,136, Visits: 12,157
Some will take exception to it, but my feeling is that if it will never me anything but a (-1), then hardcoding it makes it pretty clear when it comes to readability. I would also put a column alias on it just to increase the readability... maybe even with a comment so no one in the future has to guess.. like this (dunno if the comment is correct, though)...


--===== Insert the final result of the split
-- as a "table" instead of an "EAV" into @Ubicacion
INSERT INTO @Ubicacion (ubiIdChip, ubiIdAntena, ubiIdAntenaAnt, ubiFecha, ubiBateria)
SELECT MAX(CASE WHEN ColNum = 1 THEN ElementValue END) AS ubiIdChip,
MAX(CASE WHEN ColNum = 2 THEN ElementValue END) AS ubiIdAntena,
-1 AS ubiIdAntenaAnt, --Previous antena does not exist
CAST(CAST(MAX(CASE WHEN ColNum = 3 THEN ElementValue END) AS Float) AS DateTime) AS ubiFecha,
MAX(CASE WHEN ColNum = 4 THEN ElementValue END) AS ubiBateria
FROM @Elements
GROUP BY RowNum

Of course, you could make it self documenting with a variable/constant

DECLARE @NothingPrevious INT
SET @NothingPrevious = -1

--===== Insert the final result of the split
-- as a "table" instead of an "EAV" into @Ubicacion
INSERT INTO @Ubicacion (ubiIdChip, ubiIdAntena, ubiIdAntenaAnt, ubiFecha, ubiBateria)
SELECT MAX(CASE WHEN ColNum = 1 THEN ElementValue END) AS ubiIdChip,
MAX(CASE WHEN ColNum = 2 THEN ElementValue END) AS ubiIdAntena,
@NothingPrevious AS ubiIdAntenaAnt,
CAST(CAST(MAX(CASE WHEN ColNum = 3 THEN ElementValue END) AS Float) AS DateTime) AS ubiFecha,
MAX(CASE WHEN ColNum = 4 THEN ElementValue END) AS ubiBateria
FROM @Elements
GROUP BY RowNum

I don't believe it will matter much for speed either way, but I've not tested this bit of code for performance.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #597030
Posted Tuesday, November 04, 2008 10:19 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, April 22, 2009 8:25 PM
Points: 5, Visits: 20
Thanks Again Jeff!!

I was considering the last option you mentioned. I will do it that way...

Thanks

Jorge Luis
Post #597091
Posted Wednesday, November 05, 2008 6:06 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 8:10 PM
Points: 18,136, Visits: 12,157
Thanks for the feedback, Jorge. Let us know if there's anything else we can do to help.

--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

For better, quicker answers, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
Post #597789
« Prev Topic | Next Topic »

«««12345»»

Permissions Expand / Collapse