Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to create a stored procedure to generate a new table using SQL Server 2005?

Posted on 2012-03-12
1
Medium Priority
?
268 Views
Last Modified: 2012-03-12
I am developing a stored procedure named sp_element_attributes using SQL Server 2005 for an Access 2003 application using Access as the front end and SQL Server as the back end database. I want to create a new table titled tblElement_Attributes with the following 3 fields:

create procedure sp_element_attributes
As
Begin
create table tblElement_Attributes
(
ELEMENT_TYPE        varchar(255)   null,
ELEMENT_VALUE     varchar(255)   null,
ATTRIBUTES             varchar(255)   null
)

The result of executing the stored procedure should generate the following records and values for the newly created table tblElement_Attributes:

ELEMENT_TYPE     ELEMENT_VALUE                ATTRIBUTES
-------------------          ----------------------                               ----------------
RIGHTS                       Everything                                             NULL
RIGHTS                       Security Administrator            NULL
RIGHTS                       User                                                               NULL
RIGHTS                       View                                                              NULL

I have another table titled tblSecurity with the following fields and values:

tblSecurity

KERBEROSID      LASTNAME     FIRSTNAME    PASSWORD   RIGHTS     PASSWORDREVISED
---------------           --------------       --------------                  --------------           ---------        ---
zimmermr               ZIMMER           ROBERT               flight34               E    
smithg                       SMITH              GEORGE                 jump21                S
helmane                   HELMAN           ED                        turnip22                U
benettm                   BENETT            MARY                    ocart32                V            
trumanw                 TRUMAN          WENDY                 tert33                 S

The goal is to select the distinct values from the tblSecurity table field RIGHTS and populate them into the newly created table titled tblElement_Attributes. Thus, there will be 1 record in the newly created table tblElement_Attributes for each unique value of the field RIGHTS in table tblSecurity.

The field ELEMENT_TYPE in the tblElement_Attributes table has the static value "RIGHTS".
The field ATTRIBUTES in the tblElement_Attributes table has the static value "NULL".
The field ELEMENT_VALUE will have values as follows:

tblSecurity:  RIGHTS            tblElement_Attributes
                    ---------                                ----------------------------
                    E           ->                            Everything
                    S           ->                            Security Administrator
                    U          ->                             User
                    V          ->                             View
0
Comment
Question by:zimmer9
1 Comment
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 2000 total points
ID: 37711462
use this query in your stored procedure to insert data into the new table
 insert into tblElement_Attributes (ELEMENT_TYPE,ELEMENT_VALUE,ATTRIBUTES) 
 select distinct 'RIGHTS', 
 case (Rights) 
 WHEN 'E' THEN 'EVERYTHING' 
 WHEN 'S' THEN 'SECURITY ADMINISTRATUR' 
 WHEN 'U' THEN 'USER'
 WHEN 'V' THEN 'VIEW' 
 END , NULL from tblSecurity

Open in new window


Also you may have to put some check to create the table only if it does not exists or to drop it first and then re create it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

877 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question