473,490 Members | 2,486 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Server: Create an ID field (PK) based on two fields with a trigger-- HELP!

3 New Member
Ok, So I'm a bit new at SQL Server, and I've researched this everywhere but cannot find a clear answer and/or sample code for a solution:

-I have a table [Indicators] that has a bunch of fields including [MOE] field and a [Indicator_Selector] field, and the [INDid] field, which is the PK.

-My goal is to auto-generate the [INDid] field (PK) from concatenating the [MOE] and [Indicator_Selector] fields. so... [INDid]=[MOE]+[Indicator_Selector].

---

My first option was to make the [INDid] field a calculated field, which worked, and it even let me set it as a PK... BUT, SQL server does NOT allow this field to be a FK on a different table, which is crucial for my database. SO... I am told that a TRIGGER can do the trick, and I have found some samples on the internet, but none of them work correctly and are poorly explained.

Can anyone help me please? Thanks in advanced!!!
Dec 6 '11 #1
5 2276
Rabbit
12,516 Recognized Expert Moderator MVP
There's no need to create a third field to combine two other fields to use as a PK. You can set both fields as the PK and make it into a composite key.
Dec 7 '11 #2
newDevMan
3 New Member
Thanks rabit, and though it is possible, that is NOT an option. I NEED to have the [INDid] field, it is a foreign key for another table that joins with this one. Still need a trigger... Help!
Dec 7 '11 #3
Rabbit
12,516 Recognized Expert Moderator MVP
You can set the foreign key on the composite.
Dec 7 '11 #4
newDevMan
3 New Member
I need a trigger that generates the field.
Dec 7 '11 #5
Rabbit
12,516 Recognized Expert Moderator MVP
If you create a composite foreign key, you don't need a trigger to populate a third field. It would be simpler and the data would be more normalized.
Dec 7 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
3447
by: Martin | last post by:
Hello, I'm new with triggers and I can not find any good example on how to do the following: I have two tables WO and PM with the following fields: WO.WONUM, VARCHAR(10) WO.PMNUM,...
0
8658
by: Chris Powell | last post by:
I am using Excel/Access 2000 and have two large Excel files (25,000 rows each) that I wish to create linked tables in Access rather than importing into Access. The two source Excel files change...
0
1406
by: Ellen Ricca | last post by:
I have an Access db with several ODBC linked ORACLE tables. These tables have multiple-field PK's. The tables work just fine in many diff types of queries including unmatched queries that are...
6
2747
by: sheree | last post by:
I would like to create a query where one of the columns of the queries comes from a combo list box on a form. For example, if my table has the following fields: id name interest1 interest2...
0
1896
by: Peter S | last post by:
Hi: I am trying to read a SQL Server text field (Access memo field) using connection.OpenRecordSet("qry with text field") rather than database.OpenRecordSet("qry with text field")
0
2704
by: neoteny2 | last post by:
I need MS Access to automatically create reports/subreports based on specific criteria. I am building a database in Access 2003 with different locations/sites. I have the "sites" table created...
1
4772
by: Tony | last post by:
Hi, I have an ID text field that is composed of two alphabetical letters and the rest are numbers. This field is also a primary key. In this same table, I want to separate this field into two...
1
1859
MindBender77
by: MindBender77 | last post by:
Hello Again All, I have a memo field that is backslash delimited. I am attempting to break up this field into individual fields. Ex (memo field). \John L. Doe\\StreetName RD\\City, Pa 11111 ...
4
7998
by: learnaccess3 | last post by:
I am using a simple query (i.e.) select query taking soem fields , & binding with the controlsource property of the textbox. But I am unabel to edit the same same .Erroe Message when try to enter in...
15
4399
by: jt196 | last post by:
I'm trying to create an editable form of fulfilled orders on my system and am running into problems with creating a form based on this query. The field that I need to update (invoice number) is...
0
6974
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7183
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6852
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7356
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5448
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4573
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
628
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
277
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.