Posts
83
Comments
600
Trackbacks
40
June 2004 Entries
A "Non IDENTITY" IDENTITY Column

2 things, 1st, it's not the margartias and 2nd, anything that reads SOX sucks...I gotta read...

 

Here's the post by Derrick Leggit (to quit?)

http://www.sqlteam.com/forums/pop_profile.asp?mode=display&id=5635

And his article

http://weblogs.sqlteam.com/derrickl/archive/2004/06/28/1682.aspx

 

OK:  and the code that always seems to be asked for.  And since rembereing things ain't a strong suite (would tyhat be clubs?)

 

An old trick in non identity RDBMS's (note: back then we didn't have the niceties of triggers to make sure the identity control table didn't get out of whack)

 

USE Northwind
GO

CREATE TABLE myTable99(Col1 int PRIMARY KEY, Col2 char(1), Col3ect int)
GO

CREATE TABLE myTable99NextID(NextId int, InUseIND int)
GO

-- Set the seed

INSERT INTO myTable99NextID(NextID, InUseIND) SELECT 0,0
GO

CREATE TRIGGER myTrigger99NextID ON myTable99NextID
FOR INSERT, DELETE
AS 
  BEGIN
 IF EXISTS (SELECT * FROM inserted)
   BEGIN
  -- Nope only want to keep 1 row
    ROLLBACK
   END
 IF EXISTS (SELECT * FROM deleted)
   BEGIN
  -- Nope only want to keep 1 row
    ROLLBACK
   END
  END
GO

-- Test the trigger


INSERT INTO myTable99NextID(NextID, InUseIND) SELECT 0,0
GO

SELECT * FROM myTable99NextID

SELECT @@TRANCOUNT

DELETE FROM myTable99NextID

SELECT * FROM myTable99NextID

SELECT @@TRANCOUNT

-- Looks like we could add some error messages though

GO
CREATE PROC mySproc99 @Col2 char(1), @Col3ect int
AS
DECLARE @NextId int
SET NOCOUNT ON
  BEGIN TRAN
 UPDATE myTable99NextID SET InUseIND = 1

 SELECT @NextId = NextID + 1 FROM myTable99NextID
   
 INSERT INTO myTable99(Col1, Col2, Col3ect) VALUES(@NextId, @Col2, @Col3ect) 
 
 UPDATE myTable99NextId SET NextID = @NextId, InUseIND = 0
  COMMIT TRAN
SET NOCOUNT OFF
GO


EXEC mySproc99 'A',1

SELECT * FROM myTable99

EXEC mySproc99 'B',2

SELECT * FROM myTable99
GO

DROP PROC mySproc99
DROP TABLE myTable99
DROP TRIGGER myTrigger99NextID
DROP TABLE myTable99NextID
GO

 

posted @ Tuesday, June 29, 2004 1:55 PM | Feedback (0)
SQL SERVER TOP 10

The Top 10 questions asked (and answered) at SQL Team.  ALL helpf appreciated, and suggestions on the order of importance...

 

10.  Where the Hell is Rob

9. Where is Part 2 of More Trees and Hierarchy

8. Why is Graz in a Nun's outfit

7. They have DBAs in Australia?

6. Backup, what's a backup

5. Why is my Tansaction log 30 GB?

4. How do I put the results of dynamic SQL into a variable

3. How do I make An IN statement work with a comma delimeted string

2. How do I search an entire database for a string

1. Where can I find a margarita

 

OK, there are a lot of better real questions...I'll try and make a list of the ones we that get asked all the time.

 

 

posted @ Friday, June 18, 2004 11:30 AM | Feedback (2)
Thanks

I just had to post this...

http://www.dbforums.com/t1001863.html

And as far as Thanks Go,  It's to SQLTeam...

Best G-damn site on the planet

Rob, Nigel, Damain, Arthur, Bill, Tara...Damn the list is too long.....

Thank you everyone!

I need a new Yak Tshirt...my son confiscated my old one...

Could you imagine if SQLTeam was a consulting group?

 

 

posted @ Friday, June 18, 2004 9:23 AM | Feedback (0)
Export out entire database

For some reason this seems to be the season for people needing to dump a database to text....

 

Well Here's one way...should rewrite it to use INFORMATION_SCHEMA, but it works well enough..

If exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[isp_bcp_out_database]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[isp_bcp_out_database]
GO

CREATE PROC isp_bcp_out_database
   @dbName sysname
 , @fp varchar(255)
 , @User varchar(255)
 , @Pwd varchar(255)
AS
/*
 EXEC isp_bcp_out_database
    'Northwind'
  , 'd:\Data\Northwind\'
  , 'sa'
  , ''

*/

SET NOCOUNT ON

DECLARE bcpout CURSOR FOR
 SELECT  -- 'EXEC Master..xp_cmdshell ' +
--    '"D:\MSSQL7\Binn\bcp.exe ' + db_Name() + '.[' + TABLE_SCHEMA + '].[' + TABLE_NAME+'] '
    'bcp ' + db_Name() + '.[' + TABLE_SCHEMA + '].[' + TABLE_NAME+'] '
  + 'out ' + @fp + '\DATA\'+TABLE_SCHEMA +'_'+ REPLACE(TABLE_NAME,' ','_') + '.dat  '
  +
'-S'+@@SERVERNAME+' -U'+@User+' -P'+@Pwd+' '
  +
'-f'+@fp+'FORMAT\'+TABLE_SCHEMA +'_'+REPLACE(TABLE_NAME,' ','_')+'.fmt '
  + ' > ' + @fp + 'DATA\'+TABLE_SCHEMA +'_'+ REPLACE(TABLE_NAME,' ','_') + '.log'
  -- + ', no_output' AS CMD
   FROM    INFORMATION_SCHEMA.Tables
  WHERE    TABLE_TYPE = 'BASE TABLE'
      ORDER BY TABLE_SCHEMA, TABLE_NAME

DECLARE @CMD varchar(8000)

--create table a (id int identity(1,1), Add_Dt datetime DEFAULT GetDate(), s varchar(1000))
-- DROP TABLE a
OPEN bcpout

FETCH NEXT FROM bcpout INTO @CMD

WHILE @@FETCH_STATUS = 0
 BEGIN
  SELECT @CMD
  SELECT @CMD = 'ECHO ' + @CMD + ' > ' + @fp + '\bcpout.bat'
  EXEC master..xp_cmdshell @CMD
  SELECT @CMD = @fp + '\bcpout.bat'
  SELECT @CMD
  insert a (s)
  exec master..xp_cmdshell @cmd


  FETCH NEXT FROM bcpout INTO @CMD
 END

CLOSE bcpout
DEALLOCATE bcpout

 select id, ouputtmp = s from a

SET NOCOUNT OFF

drop table emp2

posted @ Thursday, June 17, 2004 2:16 PM | Feedback (8)
Computed Columns? Worth their Salt?

Well maybe if it was for a margarita...but me,  I prefer Rocks no salt.

But it would be an interesting poll to see how often they are used.

Me, I've never used them...and in reading BOL, seems like more of a headache than anything else.  Why not just create a function?

Anyone?

Geez what a lame post....just curious though...

FOR: 2

AGAINST: 2

 

 

posted @ Wednesday, June 16, 2004 8:59 AM | Feedback (8)
Surrogate Keys….The Devil’s spawn (OK Not really)

Always seems to spark a very heated debate…So I’d like to make a list of Pros and Cons that I’d like to keep Adding to. All comments appreciated. So I’ll start with the CON’s, mostly because I believe there are a few reasons for a surrogate key, but it is usually WAY overused and also gets dbas/developers in more trouble .

EDIT:  Ok a couple of response so far.  I think I'd like to start a Poll (If that's possible).  I'll add another pro for the devolpers in item #4.

EDIT: It's been suggested that surrogates are ANSI.  I thought they were extensions.  Anyone?

 

EDIT:  OK, it seems everyone is “on the fence“, but the original question is preferance, not wether you wouldn't use them.

POLL:

Prefer to avoid surrogates:     2

On the Fence: 6

Prefer to only use surrogates: 4

CON

1. It’s not ANSI

2. It’s not defined anywhere in the rules for a relational model or Codd’s Rules

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=33785

http://en.wikipedia.org/wiki/Ted_Codd

3. Even SQL Server itself gets in trouble because of it. Had a bunch of fun with SELECT * in a view. When the table is altered problems arise in the catalog. See item 8:  

http://weblogs.sqlteam.com/brettk/archive/2004/01/30/825.aspx

4. Excessive Need to Join to obtain values (None more glaring then one guy asking to set up identities for a zip code table.)

 5. Outside of IDENTITY, all the problems of getting the next ID for an int (The locking alone…ok, there’s always the next id table solution…but why bother)

6. You need to add constraints of attributes that should not be duplicated anyway for data integrity.

7.  People think that you can “clean up“ a table by reseting the seed...a conceptually bad idea in the relational model...AND it actualyy can be done...

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=30084

8.  Peopel do stuff like


USE Northwind
GO

SET NOCOUNT ON
CREATE TABLE myPhone99  (PhoneID  int IDENTITY(1,1), PhoneNumber  varchar(10))
CREATE TABLE myLastName99 (LastNameID  int IDENTITY(1,1), LastName  varchar(50))
CREATE TABLE myFirstName99 (FirstNameID  int IDENTITY(1,1), FirstName varchar(10))
CREATE TABLE myGender99  (GenderID  int IDENTITY(1,1), Gender  varchar(10))

CREATE TABLE myClient99(
   ClientID int IDENTITY(1,1)
 , PhoneID int
 , LastNameID int
 , FirstNameID int
 , GenderId int)
GO

INSERT INTO myGender99(Gender) SELECT 'Male' UNION ALL SELECT 'Female'
INSERT INTO myFirstName99(FirstName) SELECT 'Brett' UNION ALL SELECT 'Boy5' UNION ALL SELECT 'Tara'
INSERT INTO myLastName99(LastName) SELECT 'Kaiser' UNION ALL SELECT 'CSE' UNION ALL SELECT 'Duggan'
INSERT INTO myPhone99(PhoneNumber) SELECT '9735551212' UNION ALL SELECT '800551212'
GO

INSERT INTO myClient99(PhoneID, LastNameID, FirstNameID, GenderID)
SELECT 1,1,1,1 UNION ALL
SELECT 2,2,2,1 UNION ALL
SELECT 3,3,3,2 UNION ALL
SELECT 4,3,3,2 UNION ALL
SELECT 3,4,3,2 UNION ALL
SELECT 3,4,4,2 UNION ALL
SELECT 3,3,4,4
GO

-- Very useful, huh
SELECT * FROM myClient99
GO

-- Fun with surrogates...

   SELECT c.ClientID, p.PhoneNumber, l.LastName, f.FirstName, g.Gender
     FROM myClient99 c
LEFT JOIN myPhone99  p ON c.PhoneID     = p.PhoneID
LEFT JOIN myLastName99  l ON c.LastNameID  = l.LastNameID
LEFT JOIN myFirstName99 f ON c.FirstNameID = f.FirstNameID
LEFT JOIN myGender99 g ON c.GenderID    = g.GenderID
GO

SET NOCOUNT OFF
DROP TABLE myPhone99, myLastName99, myFirstName99, myGender99, myClient99
GO

PRO

1. Addresses. What else would be a good key? I haven’t seen one.

2. In a response it was suggested that for navigating many levels of parent child relationships it would be a good idea to use surrogate keys. The suggested number of columns was 5. I don’t consider that too unwieldy, my number would be much higher. But how deep have people seen these relationships…4, 5 or 6? I did remember long ago a logical model that a group tried to implement. Went to 15 levels. But only drives home my IDENTITY Point. A code for a code? Anyway I was honored by this reference. Please visit his site.  

http://www.xml-blog.com/archives/000234.html

3. I guess I have to put that a paper thin Index would also be desirable.

4.  Another response from developers indicating their preference.  So a PRO I guess is the overwhelming fact the people prefer this method.  I have yet to be convinced however...and this is what these blogging are all about anyway.

Please take a peek at his bolg

http://odetocode.com/Blogs/scott/archive/2004/06/09/275.aspx?Pending=true

4 1/2. Robert adds that the surogate changes so that's a plus. To me that's a debatable point...when was the last time a State changed it's name.  And even if it did, isn't that a new state?  So I'll Add it, but I have to make it item 4 1/2.

 

posted @ Wednesday, June 09, 2004 11:02 AM | Feedback (26)