Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Create View with key field

Status
Not open for further replies.

StuartBombay

Programmer
Feb 11, 2008
56
US
This may have been the wrong way to go about this, but here is what I've already done.
I have a database that tracks support tickets. There is an incident table and other tables for notes, assignments, reminders, etc. that are stored in other related tables. In order to see these entries in chronological order on a Crystal Report I created a view that uses a series of UNION ALL statements. Here is a chunk of it:

Code:
CREATE VIEW [dbo].[IncidentNotes]
AS
[green]--Get Incident[/green] 
SELECT
process.guid AS [red]'IncidentGUID'[/red],
process.title AS [red]'Title'[/red],
process.description AS [red]'Text'[/red],
process.creator AS [red]'AnalystGUID'[/red],
process.create_date AS [red]'Date'[/red],
'Incident Creation' AS [red]NoteType[/red],
user.name AS [red]'Analyst[/red]'
FROM pm_process INNER JOIN tps_user ON process.creator = user.guid
--Incident Note
UNION ALL
SELECT     
incident_note.incident_guid AS [red]'IncidentGUID'[/red],
incident_note.title AS [red]'Title'[/red],
incident_note.text AS [red]'Text'[/red],
incident_note.update_user_guid AS [red]'AnalystGUID'[/red],
incident_note.update AS [red]'Date'[/red],
'Incident Note' AS [red]NoteType[/red],
user.name AS [red]'Analyst'[/red]
FROM ' incident_note INNER JOIN  user ON incident_note.last_update_user_guid = user.guid
--Incident Reopen
UNION ALL
SELECT     
incident_reopen.incident_guid AS [red]'IncidentGUID'[/red],
incident_reopen.title AS [red]'Title'[/red],
incident_reopen.description AS [red]'Text'[/red],
incident_reopen.update_user_guid AS [red]'AnalystGUID'[/red],
incident_reopen.update AS [red]'Date'[/red],
'Reopen' AS [red]NoteType[/red],
 user.name AS [red]'Analyst'[/red]
FROM  incident_reopen INNER JOIN user ON incident_reopen.update_user_guid = user.guid

It goes on and on from there.
I need a primary key in here. Is there a way I can have the 'IncidentGUID' column be a key? There will be duplicates in there, but can it still be a key or would I create a new column that would generate a unique number?

Or, perhaps there is a better way to do this (other than a view)?
 
Well, you probably don't need a Primary key, but maybe just a Unique key. Why though? If you had some sequential number in this view, then it wouldn't really correspond to the same record every time (what happens if someone deletes a record from one of the tables, and the numbering gets shifted)? If you need a unique identifier for your reporting tool, then just try to create one with what you already have to work with in the view. Try concatenating your existing GUID's with a character corresponding to each table.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top