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

x
?
Solved

Sql Server column vakue insert

Posted on 2011-10-01
10
Medium Priority
?
190 Views
Last Modified: 2012-08-13
Hi,

In my sql server table these are the columns:

ID                 CategoryID         Description                            code
Q101           cat1                    VLC - 001 Example1              VLC - 001
Q102           cat1                    VLC - 002 Example1              VLC - 002
Q103            cat2                   PAY - 001 Example1              PAY - 001

When the user input data into this table:
(1) I wanted to generate this code depending on the CategoryID i.e. For Cat1 VLC - 001, VLC -002 and so forth
(2) For each row for the description I need to prefix the  respective code for each row they insert into table.

This data input not through a application or anything like that but they will do it in the management studio so I wanted to do this in table level in the sql server. Can I create a function and attach to this column so every time the user add a new row and will use this function to generate next available code for that row and also prefix this description as well.

Any help would very greatly appreciated....
0
Comment
Question by:ube100
  • 6
  • 4
10 Comments
 
LVL 6

Accepted Solution

by:
billfusion earned 750 total points
ID: 36897214
The easiest would be to only allow the categoryID to be entered by the user then use calculated values to insert into the rest of the columns.  Your best bet is to use a stored procedure that you pass it the ID and category id.  The procedure then sets up the proper strings either based on a lookup table, if you have many category type, or hard coded in the procedure.  The procedure then insert the proper values in the table.  The stored procedure also need to lookup the max number of records already in a table to find the correct value to append.

Calculate the Code first, then use the code to enter into the description.
0
 

Author Comment

by:ube100
ID: 36897523
This is not from a application but this is rather from a data entry by an admin staff using sql server management studio so can I still be able to call the stored procedure? If yes then how?
0
 
LVL 6

Expert Comment

by:billfusion
ID: 36897836
In that case triggers may be more appropriate.  You can find a short description about creating  triggers here:
http://msdn.microsoft.com/en-us/library/ms189799.aspx
0
Independent Software Vendors: 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!

 
LVL 6

Expert Comment

by:billfusion
ID: 36897839
A better introduction about triggers can be found here:
http://www.sqlteam.com/article/an-introduction-to-triggers-part-i
0
 
LVL 6

Expert Comment

by:billfusion
ID: 36897842
Can you include a sample of the code that you're trying to get working?
0
 
LVL 6

Expert Comment

by:billfusion
ID: 36897851
The last comment was an accident.  Please disregard it.
0
 

Author Comment

by:ube100
ID: 36897896
These are the two tables in question:

CREATE TABLE [sysdba].[CATEGORY](
      [CATEGORYID] [char](12) NOT NULL,
      [CREATEUSER] [char](12) NULL,
      [CREATEDATE] [datetime] NULL,
      [MODIFYUSER] [char](12) NULL,
      [MODIFYDATE] [datetime] NULL,
      [CATEGORYNAME] [varchar](55) NULL
) ON [PRIMARY]

CREATE TABLE [sysdba].[CALLOUTCOMES](
      [CALLOUTCOMESID] [char](12) NOT NULL,
      [CATEGORYID] [char](12) NOT NULL,
      [CREATEUSER] [char](12) NULL,
      [CREATEDATE] [datetime] NULL,
      [MODIFYUSER] [char](12) NULL,
      [MODIFYDATE] [datetime] NULL,
      [OUTCOMES] [varchar](80) NULL,
      [CALLOUTCOMECODE] [varchar](32) NULL
) ON [PRIMARY]

When they adding a new row to CALLOUTCOMES table they will use the categoryid from the category table and depending on the category we need to insert a appropriate code for CALLOUTCOMECODE column and then append that code as a prefix for the description which user entered.  i.e.  VLC - 001 Example1              VLC - 001

If we have to do this via a trigger then how we go about it please?
0
 
LVL 6

Expert Comment

by:billfusion
ID: 36898329
Can you list some row from Category, and what the user will enter in calloutcome?  is the value VLC-001 retrieved from category?  Or is it part of that code that's retrieved from category?
0
 

Author Comment

by:ube100
ID: 36900365
Category:

Q6UJ9A0RCBTK      VLC Inquires
Q6UJ9A0RCBTL      Payments

Calloutcomes:

Q6UJ9A0RCBU9      Q6UJ9A0RCBTK      U6UJ9A0000AF      2011-09-12 12:07:17.473      U6UJ9A0000AF      2011-09-12 12:07:17.473      VLC - 001 Navigation of te website      VLC - 001

Q6UJ9A0RCBUR      Q6UJ9A0RCBTL      U6UJ9A0000AF      2011-09-12 12:38:21.020      U6UJ9A0000AF      2011-09-12 12:38:21.020      PAY - 001 Monthly instalment paid      PAY - 001

VLC will represent VLC Inquiry category and anything come under that category will be VLC -001 for the first one and VLC - 002 for the 2nd one and so forth.
 
0
 

Author Closing Comment

by:ube100
ID: 37337670
That didn't help solve my issue.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

886 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