Showing posts with label business. Show all posts
Showing posts with label business. Show all posts

Tuesday, March 27, 2012

DR for Crystal Enterprise and Business Objects

Have to build DR solution and subj is a part of it.

Maybe some one has done it already… share the experience please.

Dr in my case will between 2 different data centers, replication is san 2 san so on other side I have a warm standby that does not see san disks and will become active if and only if the primary data center is down, windows domain is the same, ip addresses, names of the servers are different. How a cluster will behave is interesting as well (I mean CE is on cluster, not geo distributed cluster).

I have CE90. I’m talking to their tech support that seems to be outsourced, they didn’t really produce in 5 days anything more then a part of their own documentation in pdf format for CE version I do not use J…

So I’m wondering maybe somebody had to walk similar path already…

I see you were already involved on the previous thread that discussed some of the general considerations for such SAN replication (it was titled SAN Replication and is at http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=465113&SiteID=1). Unfortunately I'm not aware of any specific considerations for CE.

Friday, February 17, 2012

Doing a data import using DTS Wizard in SQL Server 2005 - being efficient with 5 flat files

Hi,

I'm a new user of SQL Server 2005. I have the full version installed. I also have SQL Server Business Integration Dev Studio installed. My OS is Windows XP.

I'm importing a series of 5 flat files into a database on one of the SQL Servers we have. My goal is to get 5 different tables (though perhaps I should do one and add an extra field to distinguish each import) into the database for further analysis.

I tried doing an import via DTS Wizard. There are no column names in the flat file so I defined them during the import process (all 58 of them). When I got to the end, I had an option to save the import process as a SSIS (SQL Server Integration Service) Package on:

SQL SERVER (I don't have permission for this)

or

FILE SYSTEM (did this one)

I saved the Package locally in hopes of being able to go back in, change the source file and destination table of the package and quickly get the other 4 flat files imported.

My problems are:

1) I couldn't find how to run the *.DTSX Package file to run in SQL Server Studio (basically reuse the Package with minor changes and saving me having to redefine the same 58 columns on each flat file import)

2) Tried but didn't understand how to run it in SQL Server Bus Intel Dev Studio (i.e. understanding the mapping and getting the data types right so it wouldn't error out)

3) Don't know how to make the necessary changes so that the Package handles the next source file and puts in a new destination table (do I need to do 5 CREATE TABLES so this Package has a place to run to?)

4) Does the Package need to be part of a Project to run (I haven't found how to take an existing Package and make it part of a Project/Solution)?

5) Is there a good book or online resource for just getting the basics of using SQL Server 2005 and SQL Server Business Intelligence Development Studio?

I'm really at a loss after spending a day fruitlessly on it scouring the help files, forums and experimenting around.

Hope somebody can point me in the right direction.

Regards,

Patrick Briggs,
Pasadena, CA


Patrick Briggs wrote:

My problems are:

1) I couldn't find how to run the *.DTSX Package file to run in SQL Server Studio (basically reuse the Package with minor changes and saving me having to redefine the same 58 columns on each flat file import)

You can only run from SQL Server Management Studio if it "knows" about the package. Connect to the SSIS service in SSMS and you'll see that you can tell it about packages stored on the file system and then run them from there.

Patrick Briggs wrote:

2) Tried but didn't understand how to run it in SQL Server Bus Intel Dev Studio (i.e. understanding the mapping and getting the data types right so it wouldn't error out)

You need to add the package to a project. Open VS, start a new SSIS project, and add the package to it. All of this stuff is fairly self-explanatory but if you need a hand - reply here.

Patrick Briggs wrote:

3) Don't know how to make the necessary changes so that the Package handles the next source file and puts in a new destination table (do I need to do 5 CREATE TABLES so this Package has a place to run to?)

Do you want the package to create the tables or do you want the package to assume that the tables already exist? If the former, then use the Execute SQL Task to issue a CREATE TABLE... If the latter...create the tables in SSMS and don't do anything to the package.

Patrick Briggs wrote:

4) Does the Package need to be part of a Project to run (I haven't found how to take an existing Package and make it part of a Project/Solution)?

If you want to run it in BIDS - yes. Create a new project. Right-click on the project name in SOlution Explorer. Select Add->Existing Item... and then browse to you package.

Patrick Briggs wrote:

5) Is there a good book or online resource for just getting the basics of using SQL Server 2005 and SQL Server Business Intelligence Development Studio?

What are "the basics"? There are books devoted to all the things that leverage BIDS (i.e. SSIS, SSAS, SSRS) but nothing on that only covers BIDS without the context on one of those three.

HTH

-Jamie

|||

I just spent some time working out how to do a seemingly simple task.I’m sharing the steps I took to do this in hopes it saves other SQL Server 2005 users (especially newbies like myself) time.

My original question posed on several SQL newsgroups was based on this goal:

I'm importing a series of 5 flat files (all with same file layout) into a database on one of the SQL Servers we have using SQL Server 2005 (SQL Server Management Studio) . My goal is to get 5 different tables.I want to do this without having to redo all the layout criteria 4 additional times.

Below are the steps I followed to get a solution (all done in Microsoft SQL Server Management Studio):

Create the Package (data import)

1) Use the SQL Server Import Export Wizard (equivalent to SQL Server 2000 Data Transfer Wizard) to import your first flat file.At the CHOOSE DATA SOURCE window browse for your file.

2) Under the Advanced tab, you can set your Column attributes (“output column width” or “data type” to name a few).I highlighted all the columns and selected “string [DT_STR]” for data type.To avoid truncation errors, I selected 255 for output column width.You can name the columns whose data you are most concerned with (I did import all the available fields).

3) After choosing a server destination you will have a “SELECT SOURCE TABLES AND VIEWS” window pop up.Under the “Mapping” column you can choose to tweak your mapping further editing in SQL (see Edit SQL button).I didn’t.

4) The “SAVE AND EXECUTE PACKAGE” will pop up.The “Execute Immediately” box should be checked and you should check the “Save SSIS Package” (SQL Server Integration Services).When you do, select “File System” for where to save this import-file-package to.

5) Click OKAY for the Package Protection Level and the “SAVE SSIS PACKAGE” window will appear.Browse for a path on your local computer to save to.

Modify Package (data import) for Next Use

6) In SQL Server Management Studio, browse for the Package and open it.

Preparation for SQL Task – box

7) You should see a screen that shows two boxes (“Preparation for SQL Task”) and (“Data Flow Task”).

8) Right click on the former and select “Edit”.

9) On the “SQL Statement” row, click into the right column and select the “…” box

10) Change the destination table (the table you will create with this package) to a meaningful name and click OK.

11) Click OK for the “SQL Task Editor”

Data Flow Task - box

12) Right click on the “Data Flow Task” box and select “Edit”.

13) Three boxes will appear “SourceConnectionFlatFile”, “Data Conversion 1”, and “Destination - <whatever table name your original data import went to>”.Below them is a section that displays “Connection Managers”

SourceConnectionFlatFile - editing

14) The first thing you will want to do is change the import source to a new flat file.You do this by going below the boxes under the “Connection Managers” window and right clicking on “SourceConnectionFlatFile” and then selecting “Edit”

15) Browse for the new “File Name” and select it.

16) A “Microsoft SQL Server Management Studio” window will pop up asking you if you want to “keep or reset the existing metadata”.The metadata is just your column definitions and choosing “YES” to keep this makes sense if you are doing data imports on files with the same file layout.

17) Still in the “Flat File Connection Manager Editor” window, change the “Connection Manager Name” to something meaningful (I add<_> at the end and then the name of the table the flat file is going to) and click OK.

SourceConnectionFlatFile – box (editing)

18) Right click on the“SourceConnectionFlatFile” box and select “Edit”.

19) Your newly named “Flat File Connection Manager” should appear in select box.

20) Click OK, right click again on the “SourceConnectionFlatFile” box and select “Show Advanced Editor”.

21) Under the “Connections Manager” tab, your newly named “Flat File Connection” should appear (the prior step is necessary for the advanced editor to recognize your change).

22) Under the “Component Properties” tab, on the “Name” row, click into the right column and rename to something meaningful (notice the “Identification String” row description changes too once you click out of the “Name” row)

23) Under the “Column Mappings” tab, just confirm you are mapping your flat file fields (“Available External Columns”) to a destination table’s fields (“Available Output Columns”).

24) Under the “Input and Output Properties” tab you can check in “Flat File Source Output” to make modifications to either your “External Columns” or your “Output Columns” – you shouldn’t need to for a simple import.
((NOTE: any changes you make here would likely need to be consistent with the column properties found under the “Connection Manager Window” for the “SourceConnectionFlatFile” as well as the “Data Conversion 1” box under the “Data Flow Tasks” window, so exercise caution

25) NOTE:This process has worked for me by making my source columns all “string [DT_STR]” data type and the output columns all “Unicode String [DT_WSTR]” data type.

Data Conversion 1 – box (editing)

26) There is nothing you need to do here.By right clicking on the “Data Conversion 1” box and selecting “Edit”, you can see and change the data type of the output columns (the ones in the table your importing the flat file to).There are probably more edits one can do but they’re beyond what I’ve learned.

Destination - <whatever table name your original data import went to> – box (editing)

27) Right click on the “Destination - <whatever table name your original data import went to>” box and select “Show Advanced Editor”.

28) Select the “Component Properties” tab.

29) Select the right column at the “Name” row and change the name to something meaningful (ie. related to the source file name or the table name you’re importing to).

30) Select the right column at the “Identification String” row and it will update to this change.

31) Select the right column at the “OpenRowSet” and change it to the name of the table you are importing your flat file to (this should be consistent with table name under step 10).

32) Click OK

33) Select FILE and select “Save As…” and then give your package a new name that’s meaningful (this will be helpful if you have to rerun the import of the flat file later).

Run (execute) the Revised Package (data import)

34) Go back to SQL Server Management Studio and open the Object Explorer

35) Connect to an “Integration Services” component.This should essentially be a local instance (not sure where it is on the local computer or in SQL Server Management Studio on the local computer).

36) In “Object Explorer” go down to your “Integration Services” object and expand it.

37) Expand “Stored Packages”

38) Right click on “File System” and select “Import Package” and an “IMPORT PACKAGE” window will appear

39) For “Package Location” choose “File System” and then browse for the “Package Path”

40) Click into the “Package Name” and it defaults to your Package’s file name.

41) Click OK and the Package is imported.

42) Right click on the newly imported Package and select “Run Package”

43) An “Execute Package Utility” window appears

44) Select “Execute” and the package runs.

|||Patrick, Can you do this for multiple files in one shot or do you have to go thru these steps separately for every file? Please let me know...I have around 300 flat files with diff. layouts so I am trying to figure out the best way to do this. Thanks!|||I never really tried it for multiple files at once. I am new to SQL Server 2005. I've noticed that as I've used this means of doing multiple imports in my work, it's only time efficient if the files are many columns with varying data types.

You bring up a good question which I hope somebody here will try to answer. The steps I outlined are pretty tedious to do file after file.

Regards,

Patrick in Pasadena
|||What's wrong with containing the data flow in a foreach loop that opens each file?|||

Patrick Briggs wrote:

I never really tried it for multiple files at once. I am new to SQL Server 2005. I've noticed that as I've used this means of doing multiple imports in my work, it's only time efficient if the files are many columns with varying data types.

You bring up a good question which I hope somebody here will try to answer. The steps I outlined are pretty tedious to do file after file.

Regards,

Patrick in Pasadena

I've not followed this thread completely but if you're talking about loading multiple files, this link may help:

Processing data from multiple files all at once
(http://blogs.conchango.com/jamiethomson/archive/2006/10/14/SSIS_3A00_-Processing-data-from-multiple-files-all-at-once.aspx)

-Jamie

Doing a data import using DTS Wizard in SQL Server 2005 - being efficient with 5 flat files

Hi,

I'm a new user of SQL Server 2005. I have the full version installed. I also have SQL Server Business Integration Dev Studio installed. My OS is Windows XP.

I'm importing a series of 5 flat files into a database on one of the SQL Servers we have. My goal is to get 5 different tables (though perhaps I should do one and add an extra field to distinguish each import) into the database for further analysis.

I tried doing an import via DTS Wizard. There are no column names in the flat file so I defined them during the import process (all 58 of them). When I got to the end, I had an option to save the import process as a SSIS (SQL Server Integration Service) Package on:

SQL SERVER (I don't have permission for this)

or

FILE SYSTEM (did this one)

I saved the Package locally in hopes of being able to go back in, change the source file and destination table of the package and quickly get the other 4 flat files imported.

My problems are:

1) I couldn't find how to run the *.DTSX Package file to run in SQL Server Studio (basically reuse the Package with minor changes and saving me having to redefine the same 58 columns on each flat file import)

2) Tried but didn't understand how to run it in SQL Server Bus Intel Dev Studio (i.e. understanding the mapping and getting the data types right so it wouldn't error out)

3) Don't know how to make the necessary changes so that the Package handles the next source file and puts in a new destination table (do I need to do 5 CREATE TABLES so this Package has a place to run to?)

4) Does the Package need to be part of a Project to run (I haven't found how to take an existing Package and make it part of a Project/Solution)?

5) Is there a good book or online resource for just getting the basics of using SQL Server 2005 and SQL Server Business Intelligence Development Studio?

I'm really at a loss after spending a day fruitlessly on it scouring the help files, forums and experimenting around.

Hope somebody can point me in the right direction.

Regards,

Patrick Briggs,
Pasadena, CA


Patrick Briggs wrote:

My problems are:

1) I couldn't find how to run the *.DTSX Package file to run in SQL Server Studio (basically reuse the Package with minor changes and saving me having to redefine the same 58 columns on each flat file import)

You can only run from SQL Server Management Studio if it "knows" about the package. Connect to the SSIS service in SSMS and you'll see that you can tell it about packages stored on the file system and then run them from there.

Patrick Briggs wrote:

2) Tried but didn't understand how to run it in SQL Server Bus Intel Dev Studio (i.e. understanding the mapping and getting the data types right so it wouldn't error out)

You need to add the package to a project. Open VS, start a new SSIS project, and add the package to it. All of this stuff is fairly self-explanatory but if you need a hand - reply here.

Patrick Briggs wrote:

3) Don't know how to make the necessary changes so that the Package handles the next source file and puts in a new destination table (do I need to do 5 CREATE TABLES so this Package has a place to run to?)

Do you want the package to create the tables or do you want the package to assume that the tables already exist? If the former, then use the Execute SQL Task to issue a CREATE TABLE... If the latter...create the tables in SSMS and don't do anything to the package.

Patrick Briggs wrote:

4) Does the Package need to be part of a Project to run (I haven't found how to take an existing Package and make it part of a Project/Solution)?

If you want to run it in BIDS - yes. Create a new project. Right-click on the project name in SOlution Explorer. Select Add->Existing Item... and then browse to you package.

Patrick Briggs wrote:

5) Is there a good book or online resource for just getting the basics of using SQL Server 2005 and SQL Server Business Intelligence Development Studio?

What are "the basics"? There are books devoted to all the things that leverage BIDS (i.e. SSIS, SSAS, SSRS) but nothing on that only covers BIDS without the context on one of those three.

HTH

-Jamie

|||

I just spent some time working out how to do a seemingly simple task.I’m sharing the steps I took to do this in hopes it saves other SQL Server 2005 users (especially newbies like myself) time.

My original question posed on several SQL newsgroups was based on this goal:

I'm importing a series of 5 flat files (all with same file layout) into a database on one of the SQL Servers we have using SQL Server 2005 (SQL Server Management Studio) . My goal is to get 5 different tables.I want to do this without having to redo all the layout criteria 4 additional times.

Below are the steps I followed to get a solution (all done in Microsoft SQL Server Management Studio):

Create the Package (data import)

1) Use the SQL Server Import Export Wizard (equivalent to SQL Server 2000 Data Transfer Wizard) to import your first flat file.At the CHOOSE DATA SOURCE window browse for your file.

2) Under the Advanced tab, you can set your Column attributes (“output column width” or “data type” to name a few).I highlighted all the columns and selected “string [DT_STR]” for data type.To avoid truncation errors, I selected 255 for output column width.You can name the columns whose data you are most concerned with (I did import all the available fields).

3) After choosing a server destination you will have a “SELECT SOURCE TABLES AND VIEWS” window pop up.Under the “Mapping” column you can choose to tweak your mapping further editing in SQL (see Edit SQL button).I didn’t.

4) The “SAVE AND EXECUTE PACKAGE” will pop up.The “Execute Immediately” box should be checked and you should check the “Save SSIS Package” (SQL Server Integration Services).When you do, select “File System” for where to save this import-file-package to.

5) Click OKAY for the Package Protection Level and the “SAVE SSIS PACKAGE” window will appear.Browse for a path on your local computer to save to.

Modify Package (data import) for Next Use

6) In SQL Server Management Studio, browse for the Package and open it.

Preparation for SQL Task – box

7) You should see a screen that shows two boxes (“Preparation for SQL Task”) and (“Data Flow Task”).

8) Right click on the former and select “Edit”.

9) On the “SQL Statement” row, click into the right column and select the “…” box

10) Change the destination table (the table you will create with this package) to a meaningful name and click OK.

11) Click OK for the “SQL Task Editor”

Data Flow Task - box

12) Right click on the “Data Flow Task” box and select “Edit”.

13) Three boxes will appear “SourceConnectionFlatFile”, “Data Conversion 1”, and “Destination - <whatever table name your original data import went to>”.Below them is a section that displays “Connection Managers”

SourceConnectionFlatFile - editing

14) The first thing you will want to do is change the import source to a new flat file.You do this by going below the boxes under the “Connection Managers” window and right clicking on “SourceConnectionFlatFile” and then selecting “Edit”

15) Browse for the new “File Name” and select it.

16) A “Microsoft SQL Server Management Studio” window will pop up asking you if you want to “keep or reset the existing metadata”.The metadata is just your column definitions and choosing “YES” to keep this makes sense if you are doing data imports on files with the same file layout.

17) Still in the “Flat File Connection Manager Editor” window, change the “Connection Manager Name” to something meaningful (I add<_> at the end and then the name of the table the flat file is going to) and click OK.

SourceConnectionFlatFile – box (editing)

18) Right click on the“SourceConnectionFlatFile” box and select “Edit”.

19) Your newly named “Flat File Connection Manager” should appear in select box.

20) Click OK, right click again on the “SourceConnectionFlatFile” box and select “Show Advanced Editor”.

21) Under the “Connections Manager” tab, your newly named “Flat File Connection” should appear (the prior step is necessary for the advanced editor to recognize your change).

22) Under the “Component Properties” tab, on the “Name” row, click into the right column and rename to something meaningful (notice the “Identification String” row description changes too once you click out of the “Name” row)

23) Under the “Column Mappings” tab, just confirm you are mapping your flat file fields (“Available External Columns”) to a destination table’s fields (“Available Output Columns”).

24) Under the “Input and Output Properties” tab you can check in “Flat File Source Output” to make modifications to either your “External Columns” or your “Output Columns” – you shouldn’t need to for a simple import.
((NOTE: any changes you make here would likely need to be consistent with the column properties found under the “Connection Manager Window” for the “SourceConnectionFlatFile” as well as the “Data Conversion 1” box under the “Data Flow Tasks” window, so exercise caution

25) NOTE:This process has worked for me by making my source columns all “string [DT_STR]” data type and the output columns all “Unicode String [DT_WSTR]” data type.

Data Conversion 1 – box (editing)

26) There is nothing you need to do here.By right clicking on the “Data Conversion 1” box and selecting “Edit”, you can see and change the data type of the output columns (the ones in the table your importing the flat file to).There are probably more edits one can do but they’re beyond what I’ve learned.

Destination - <whatever table name your original data import went to> – box (editing)

27) Right click on the “Destination - <whatever table name your original data import went to>” box and select “Show Advanced Editor”.

28) Select the “Component Properties” tab.

29) Select the right column at the “Name” row and change the name to something meaningful (ie. related to the source file name or the table name you’re importing to).

30) Select the right column at the “Identification String” row and it will update to this change.

31) Select the right column at the “OpenRowSet” and change it to the name of the table you are importing your flat file to (this should be consistent with table name under step 10).

32) Click OK

33) Select FILE and select “Save As…” and then give your package a new name that’s meaningful (this will be helpful if you have to rerun the import of the flat file later).

Run (execute) the Revised Package (data import)

34) Go back to SQL Server Management Studio and open the Object Explorer

35) Connect to an “Integration Services” component.This should essentially be a local instance (not sure where it is on the local computer or in SQL Server Management Studio on the local computer).

36) In “Object Explorer” go down to your “Integration Services” object and expand it.

37) Expand “Stored Packages”

38) Right click on “File System” and select “Import Package” and an “IMPORT PACKAGE” window will appear

39) For “Package Location” choose “File System” and then browse for the “Package Path”

40) Click into the “Package Name” and it defaults to your Package’s file name.

41) Click OK and the Package is imported.

42) Right click on the newly imported Package and select “Run Package”

43) An “Execute Package Utility” window appears

44) Select “Execute” and the package runs.

|||Patrick, Can you do this for multiple files in one shot or do you have to go thru these steps separately for every file? Please let me know...I have around 300 flat files with diff. layouts so I am trying to figure out the best way to do this. Thanks!|||I never really tried it for multiple files at once. I am new to SQL Server 2005. I've noticed that as I've used this means of doing multiple imports in my work, it's only time efficient if the files are many columns with varying data types.

You bring up a good question which I hope somebody here will try to answer. The steps I outlined are pretty tedious to do file after file.

Regards,

Patrick in Pasadena|||What's wrong with containing the data flow in a foreach loop that opens each file?|||

Patrick Briggs wrote:

I never really tried it for multiple files at once. I am new to SQL Server 2005. I've noticed that as I've used this means of doing multiple imports in my work, it's only time efficient if the files are many columns with varying data types.

You bring up a good question which I hope somebody here will try to answer. The steps I outlined are pretty tedious to do file after file.

Regards,

Patrick in Pasadena

I've not followed this thread completely but if you're talking about loading multiple files, this link may help:

Processing data from multiple files all at once
(http://blogs.conchango.com/jamiethomson/archive/2006/10/14/SSIS_3A00_-Processing-data-from-multiple-files-all-at-once.aspx)

-Jamie

Tuesday, February 14, 2012

Does this violate normal form?

Person A is a customer
Person A is a vendor
Person A works out of their home
customer and vendor share an address file
Person A has a B (business) type record on address table
Person A has a H (home) type record on address table that
matches the business record (except for type)
Does this violate normal form?"DazedAndConfused" <AceMagoo61@.yahoo.com> wrote in message
news:%23OXa9phvFHA.1988@.TK2MSFTNGP10.phx.gbl...
> Person A is a customer
> Person A is a vendor
> Person A works out of their home
> customer and vendor share an address file
> Person A has a B (business) type record on address table
> Person A has a H (home) type record on address table that
> matches the business record (except for type)
>
> Does this violate normal form?
>
That's just some english: normal forms don't enter into it.
David|||Since you've told us nothing about keys or functional dependencies how can
we tell?
David Portas
SQL Server MVP
--|||On Tue, 20 Sep 2005 15:35:39 -0400, "DazedAndConfused"
<AceMagoo61@.yahoo.com> wrote:
>Person A is a customer
>Person A is a vendor
>Person A works out of their home
>customer and vendor share an address file
>Person A has a B (business) type record on address table
>Person A has a H (home) type record on address table that
>matches the business record (except for type)
>
>Does this violate normal form?
Well, if what you're saying is that you have:
MyID FKID Type Address
-- -- -- ---
1 123 B 100 Main Street
2 123 H 100 Main Street
With a duplicate address entered twice, each with different type, then
I'm not sure that anything has been violated, it would seem a semantic
issue.
Josh
ps - no the MyId field is not needed, but y'know how it is these days.|||Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.
Let's get back to the basics of an RDBMS. Rows are not records; fields
are not columns; tables are not files.
\|||This is not a debug issue, it is a learning experiance, in an earlier post
you indicated using the IDENTITY column wasn't such a good thing.
Not really sure what you mean. Right now I use IDENTITY on the person table
to generate the personID, personID is then assigned to child rows in the
users, customers, employees, phone, email, address tables.
Should I be generating custom assignments instead of using IDENTITY? If I
sometimes sound like I'm from the 1950's it is because I am trying to switch
from Mainframe/VSAM/CICS to .NET. Now is the time to learn the right way.
How bad is it?
-- User Table
CREATE TABLE users
( personID int not null,
userID varchar(10) not null,
pw varchar(10) not null,
type char(1) not null,
sec_lev char(1) not null,
effective smalldatetime not null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_usersID PRIMARY KEY CLUSTERED(userID),
CONSTRAINT FK_updateby FOREIGN KEY (updateby) REFERENCES users (userID),
CONSTRAINT UserMustBeUnique UNIQUE (userid)
)
go
-- Person Table
CREATE TABLE person
( personID int IDENTITY (1,1) not null ,
prefix nvarchar(6) null,
firstname varchar(15) not null,
MI char(1) null,
lastname varchar(20) not null,
suffix nvarchar(6) null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_personID PRIMARY KEY CLUSTERED(personID),
CONSTRAINT FK_pers_prefix FOREIGN KEY (prefix) REFERENCES prefix(prefix),
CONSTRAINT FK_pers_suffix FOREIGN KEY (suffix) REFERENCES suffix(suffix),
CONSTRAINT FK_pers_updateby FOREIGN KEY (updateby) REFERENCES users
(userID)
)
go
CREATE UNIQUE INDEX person_name_ind
ON person (prefix, firstname, lastname, MI, suffix)
go
ALTER TABLE users
ADD CONSTRAINT FK_users_personID FOREIGN KEY (personID) REFERENCES
person(personID)
go
-- Insert into person table
ALTER TABLE person
NOCHECK CONSTRAINT FK_pers_updateby
go
DECLARE @.userID varchar(10)
SET @.userID = 'master'
DECLARE @.myDate smalldatetime
SET @.myDate = getdate()
INSERT INTO person (firstname, lastname, suffix, lastupdate, updateby)
VALUES ('Master', 'Blaster', 'Sr.', @.myDate, @.userID)
go
DECLARE @.userID varchar(10)
SET @.userID = 'testuser'
DECLARE @.myDate smalldatetime
SET @.myDate = DATEADD(mi, 1, getdate())
INSERT INTO person (prefix, firstname, lastname, suffix, lastupdate,
updateby)
VALUES ('Mr.', 'Test', 'User', 'Sr.', @.myDate, @.userID)
go
SELECT * FROM person
DECLARE @.date smalldatetime
set @.date = DATEADD(mi, 3, getdate())
INSERT INTO person (prefix,firstname,lastname,suffix,lastup
date,updateby)
VALUES ('Mrs.','Test','User','Sr.', @.date,'mtestuser')
go
ALTER TABLE person
CHECK CONSTRAINT FK_pers_updateby
go
SELECT * FROM person
go
-- Insert records into users table
DECLARE @.userID varchar(10)
SET @.userID = 'master'
DECLARE @.personID int
SET @.personID = (SELECT personID FROM person WHERE updateby = @.userID)
DECLARE @.myDate smalldatetime
SET @.myDate = (SELECT lastupdate FROM person WHERE personID = @.personID)
INSERT INTO users (personID, userID, pw, type, sec_lev, effective,
lastupdate, updateby)
VALUES (@.personID, @.userID, 'masterpw', UPPER('e'), 'A', @.myDate, @.myDate,
@.userID)
go
DECLARE @.userID varchar(10)
SET @.userID = 'testuser'
DECLARE @.personID int
SET @.personID = (SELECT personID FROM person WHERE updateby = @.userID)
DECLARE @.myDate smalldatetime
SET @.myDate = (SELECT lastupdate FROM person WHERE personID = @.personID)
INSERT INTO users (personID, userID, pw, type, sec_lev, effective,
lastupdate, updateby)
VALUES (@.personID, @.userID, 'testpw', UPPER('c'), 'C', @.myDate, @.myDate,
@.userID)
go
SELECT * FROM person
SELECT * FROM users
go
-- Customer Table
CREATE TABLE customer
( personID int not null,
userID varchar(10) null,
since smalldatetime not null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_cust_personID PRIMARY KEY CLUSTERED(personID),
CONSTRAINT FK_cust_personID FOREIGN KEY (personID) REFERENCES
person(personID),
CONSTRAINT FK_cust_updateby FOREIGN KEY (updateby) REFERENCES
users(userID)
)
go
CREATE INDEX cust_userID_ind
ON customer (userID)
go
-- Insert records into customer table
DECLARE @.personID int
SET @.personID =
(SELECT personID FROM person
WHERE (prefix = 'Mr.' AND firstname = 'Test' And lastname = 'User')
)
DECLARE @.myDate smalldatetime
SET @.myDate =
(SELECT lastupdate FROM person
WHERE personID = @.personID)
DECLARE @.userID varchar(10)
SET @.userID =
(SELECT updateby FROM person
WHERE personID = @.personID)
INSERT INTO customer (personID, userID, since, lastupdate, updateby)
VALUES (@.personID, @.userID, @.myDate, @.myDate, @.userID)
go
SELECT * FROM users
SELECT * FROM person
SELECT * FROM customer
go
-- Employee Table
CREATE TABLE employee
( personID int not null,
empssi char(11) not null,
dob smalldatetime null,
license varchar(15) null,
licstate char(2) null,
userID varchar(10) null,
region char(3) null,
dept char(3) null,
position char(3) null,
postype char(1) null,
hiredate smalldatetime not null,
termdate smalldatetime null,
salary money not null,
freq char(1) not null,
notes varchar(1) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
-- Primary Key id is assigned from person table personID
CONSTRAINT PK_emp_personID PRIMARY KEY CLUSTERED(personID),
-- Driver licence state must be in states table
CONSTRAINT FK_lic_state FOREIGN KEY (licstate) REFERENCES
states(state_abrv),
-- Employee personID PRIMARY field must have valid person.personID
CONSTRAINT FK_emp_personID FOREIGN KEY (personID) REFERENCES
person(personID),
-- updateby field must be valid userID
CONSTRAINT FK_emp_updateby FOREIGN KEY (updateby) REFERENCES
users(userID)
)
go
-- Employee SSI number index, index must be unique
CREATE UNIQUE INDEX empssi_ind
ON employee (empssi)
go
-- Employee userID index
CREATE INDEX emp_userID_ind
ON employee (userID)
go
DECLARE @.personID int
SET @.personID =
(SELECT personID FROM person
WHERE (firstname = 'Master' And lastname = 'Blaster')
)
DECLARE @.myDate smalldatetime
SET @.myDate = (SELECT lastupdate FROM person WHERE personID = @.personID)
DECLARE @.userID varchar(10)
SET @.userID = (SELECT updateby FROM person WHERE personID = @.personID)
INSERT INTO employee (personID, empssi, dob, license, licstate, userID,
region, dept, position, postype, hiredate, salary, freq,
lastupdate, updateby)
VALUES (@.personID, '003-91-9823', '6/12/1960', '06BRM60121', 'NH', @.userID,
'C', 'IT', 'DBA', 'S', '10/18/1993', $78000, 'B', @.myDate, @.userID)
go
SELECT * FROM users
SELECT * FROM person
SELECT * FROM customer
SELECT * FROM employee
go
-- Create communication preferance table
-- type H=Home, B=Business,C=Cell, etc.
-- preference P=Phone, E=Email, M=Mail
CREATE TABLE commPref
( personID int not null,
type char(1) not null,
preference char(1) not null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_CP_personID PRIMARY KEY CLUSTERED(personID),
CONSTRAINT FK_CP_personID FOREIGN KEY (personID) REFERENCES
person(personID),
CONSTRAINT FK_CP_updateby FOREIGN KEY (updateby) REFERENCES users (userID)
)
go
-- Insert records into communication preference table
DECLARE @.personID int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Testuser')
)
DECLARE @.myDate smalldatetime
SET @.myDate =
(SELECT lastupdate FROM person
WHERE personID = @.personID)
DECLARE @.userID varchar(10)
SET @.userID =
(SELECT updateby FROM person
WHERE personID = @.personID)
INSERT INTO commPref (personID, type, preference, lastupdate, updateby)
VALUES(@.personID, 'H', 'P', @.myDate, @.userID)
go
DECLARE @.personID int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
DECLARE @.myDate smalldatetime
SET @.myDate =
(SELECT lastupdate FROM person
WHERE personID = @.personID)
DECLARE @.userID varchar(10)
SET @.userID =
(SELECT updateby FROM person
WHERE personID = @.personID)
INSERT INTO commPref (personID, type, preference, lastupdate, updateby)
VALUES(@.personID, 'B', 'P', @.myDate, @.userID)
go
SELECT * FROM users
SELECT * FROM person
SELECT * FROM customer
SELECT * FROM employee
SELECT * FROM commPref
go
CREATE TABLE phone
( personID int not null,
type char(1) not null,
seq int not null,
preferred char(1) not null,
phone char(13) not null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_phone UNIQUE NONCLUSTERED(personID, type, seq),
CONSTRAINT FK_ph_personID FOREIGN KEY (personID) REFERENCES
person(personID),
CONSTRAINT FK_ph_updateby FOREIGN KEY (updateby) REFERENCES users(userID),
CONSTRAINT invPhoneType CHECK (type LIKE '[HBC]'),
CONSTRAINT invPhonePref CHECK (preferred LIKE '[YN]'),
CONSTRAINT invPhonePattern CHECK (phone LIKE
'([0-9][0-9][0-9])[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9]'),
)
go
-- Phone personID index
CREATE CLUSTERED INDEX ph_personID_ind
ON phone (personID)
go
-- Phone index
CREATE INDEX phone_ind
ON phone (phone)
go
-- Insert records into phone table
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM phone
WHERE (personID = @.personID AND type = 'B')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO phone (personID, type, seq, preferred, phone, lastupdate,
updateby)
VALUES (@.personID, 'B', @.seq, 'Y', '(555)555-1212', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = DATEADD(mi, 1, getdate())
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM phone
WHERE (personID = @.personID AND type = 'H')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO phone (personID, type, seq, preferred, phone, lastupdate,
updateby)
VALUES (@.personID, 'H', @.seq, 'N', '(555)555-1112', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = DATEADD(mi, 2, getdate())
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM phone
WHERE (personID = @.personID AND type = 'C')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO phone (personID, type, seq, preferred, phone, lastupdate,
updateby)
VALUES (@.personID, 'C', @.seq, 'N', '(555)555-1122', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = DATEADD(mi, 3, getdate())
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM phone
WHERE (personID = @.personID AND type = 'C')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO phone (personID, type, seq, preferred, phone, lastupdate,
updateby)
VALUES (@.personID, 'C', @.seq, 'N', '(555)555-1222', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Testuser')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM phone
WHERE (personID = @.personID AND type = 'H')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO phone (personID, type, seq, preferred, phone, lastupdate,
updateby)
VALUES (@.personID, 'H', @.seq, 'Y', '(555)555-2121', @.myDate, @.userID)
go
SELECT * FROM users
SELECT * FROM person
SELECT * FROM customer
SELECT * FROM employee
SELECT * FROM commPref
SELECT * FROM phone
go
CREATE TABLE email
( personID int not null,
type char(1) not null,
seq int not null,
preferred char(1) not null,
e_mail varchar(35) not null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_email UNIQUE NONCLUSTERED (personID, type, seq),
CONSTRAINT FK_email_personID FOREIGN KEY (personID) REFERENCES
person(personID),
CONSTRAINT FK_email_updateby FOREIGN KEY (updateby) REFERENCES
users(userID),
CONSTRAINT invEmailPattern CHECK (e_mail LIKE '%@.%.%'),
CONSTRAINT invEmailType CHECK (type LIKE '[HB]'),
CONSTRAINT invEmailPref CHECK (preferred LIKE '[YN]')
)
go
-- Email personID index
CREATE CLUSTERED INDEX em_personID_ind
ON email (personID)
go
-- Email index
CREATE INDEX email_ind
ON email (e_mail)
go
-- Insert records into email table
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM email
WHERE (personID = @.personID AND type = 'B')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO Email (personID, type, seq, preferred, e_mail, lastupdate,
updateby)
VALUES (@.personID, 'B', @.seq, 'Y', 'master@.oo.com', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = getdate()
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM email
WHERE (personID = @.personID AND type = 'B')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO Email (personID, type, seq, preferred, e_mail, lastupdate,
updateby)
VALUES (@.personID, 'B', @.seq, 'N', 'dba@.oo.com', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = DATEADD(mi, 3, getdate())
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM email
WHERE (personID = @.personID AND type = 'H')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO Email (personID, type, seq, preferred, e_mail, lastupdate,
updateby)
VALUES (@.personID, 'H', @.seq, 'N', 'mblaster@.oo.com', @.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10), @.seq
int
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Testuser')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
SET @.seq =
(SELECT max(seq) FROM email
WHERE (personID = @.personID AND type = 'H')
)
IF @.seq is null
SET @.seq = 1
ELSE
SET @.seq = @.seq + 1
INSERT INTO Email (personID, type, seq, preferred, e_mail, lastupdate,
updateby)
VALUES (@.personID, 'H', @.seq, 'Y', 'tuser@.oo.com', @.myDate, @.userID)
go
SELECT * FROM users
SELECT * FROM person
SELECT * FROM customer
SELECT * FROM employee
SELECT * FROM commPref
SELECT * FROM phone
SELECT * FROM email
go
CREATE TABLE address
( personID int not null,
type char(1) not null,
address1 varchar(25) not null,
address2 varchar(25) null,
city varchar(20) not null,
state char(2) not null,
zip varchar(5) not null,
zip4 varchar(4) Null,
notes varchar(1000) null,
lastupdate smalldatetime not null,
updateby varchar(10) not null,
CONSTRAINT PK_addr_personID PRIMARY KEY NONCLUSTERED(personID, type),
-- Address personID PRIMARY column must have valid person.personID
CONSTRAINT FK_addr_personID FOREIGN KEY (personID) REFERENCES
person(personID),
CONSTRAINT FK_addr_states FOREIGN KEY (state) REFERENCES
states(state_abrv),
CONSTRAINT FK_addr_updateby FOREIGN KEY (updateby) REFERENCES
users(userID),
CONSTRAINT invAddrType CHECK (type LIKE '[HBAOTV]'),
CONSTRAINT zipMustBeNumeric CHECK (zip LIKE '[0-9][0-9][0-9][0-9][0-9]'),
CONSTRAINT zip4MustBeNumeric CHECK (zip4 LIKE '[0-9][0-9][0-9][0-9]')
)
go
-- Address personID index
CREATE CLUSTERED INDEX addr_personID_ind
ON address (personID)
go
-- Address personID index
CREATE INDEX addr_address_ind
ON address (address1, zip)
go
-- Insert records into address table
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10)
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Testuser')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
INSERT INTO address (personID, type, address1, city, state, zip, lastupdate,
updateby)
VALUES (@.personID, 'H', '14 Testers HWY.', 'Testville', 'RI', '99999',
@.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10)
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate =
(SELECT lastupdate
FROM person
WHERE personID = @.personID)
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
INSERT INTO address (personID, type, address1, city, state, zip, lastupdate,
updateby)
VALUES (@.personID, 'B', '1 Pizza Rd.', 'Pizzaville', 'RI', '19999',
@.myDate, @.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10)
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = getdate()
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
INSERT INTO address (personID, type, address1, city, state, zip, lastupdate,
updateby)
VALUES (@.personID, 'H', '8 DBA Blvd.', 'Dibaville', 'RI', '29999', @.myDate,
@.userID)
go
DECLARE @.personID int, @.myDate smalldatetime, @.userID as varchar(10)
SET @.personID =
(SELECT personID FROM person
WHERE (updateby = 'Master')
)
SET @.myDate = DATEADD(mi, 2, getdate())
SET @.userID =
(SELECT updateby
FROM person
WHERE personID = @.personID)
INSERT INTO address (personID, type, address1, city, state, zip, lastupdate,
updateby)
VALUES (@.personID, 'V', 'Relax Ln.', 'Offville', 'RI', '39999', @.myDate,
@.userID)
go
"--CELKO--" <jcelko212@.earthlink.net> wrote in message
news:1127275885.339847.72700@.z14g2000cwz.googlegroups.com...
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, data types, etc. in
> your schema are. Sample data is also a good idea, along with clear
> specifications. It is very hard to debug code when you do not let us
> see it.
> Let's get back to the basics of an RDBMS. Rows are not records; fields
> are not columns; tables are not files.
> \
>