Solved

Sql Server column vakue insert

Posted on 2011-10-01
10
180 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 250 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
 
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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now