Question

So I have some tables that contains a foreign key that refers to different tables and fields depending on the context. I have a key source table that contains the relevant table and field names. Then I need to be able to lookup the values in those tables/fields.

I simplified it to the following tables/data for demonstration of what I have:

CREATE TABLE [dbo].[WorkOrderStatus](
    [WorkOrderStatusId] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](60) NOT NULL
) ON [PRIMARY]

CREATE TABLE [dbo].[Note](
    [NoteId] [int] IDENTITY(1,1) NOT NULL,
    [NoteKeySourceId] [int] NULL,
    [KeyValue] [int] NULL,
    [Note] [nvarchar](max) NOT NULL,
) ON [PRIMARY]

CREATE TABLE [dbo].[NoteKeySource](
    [NoteKeySourceId] [int] IDENTITY(1,1) NOT NULL,
    [SourceTable] [nvarchar](60) NOT NULL,
    [SourceField] [nvarchar](60) NOT NULL,
    [SourceTextField] [nvarchar](60) NOT NULL,
) ON [PRIMARY]

GO

INSERT INTO WorkOrderStatus VALUES('Open')
DECLARE @OpenStatusId int = SCOPE_IDENTITY();

INSERT INTO WorkOrderStatus VALUES('Closed')
DECLARE @ClosedStatusId int = SCOPE_IDENTITY();

INSERT INTO NoteKeySource VALUES('WorkOrderStatus', 'WorkOrderStatusId', 'Name')
DECLARE @WorkOrderSource int = SCOPE_IDENTITY();

INSERT INTO Note VALUES(@WorkOrderSource, @OpenStatusId, 'Opened the work order')
INSERT INTO Note VALUES(@WorkOrderSource, @ClosedStatusId, 'Closed the work order')

So I need to be able to query a list of Notes, including the SourceTextField referred to in NoteKeySource, identified by KeyValue.

Here is the SP that I have now, which works:

CREATE PROCEDURE [dbo].[spGetNotes]
AS
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

CREATE TABLE #NoteTable (
    NoteId int NOT NULL,
    SourceTable nvarchar(60),
    SourceField nvarchar(60),
    SourceTextField nvarchar(60),
    KeyValue int,
    Context nvarchar(MAX),
    Note nvarchar(MAX),
)

INSERT INTO #NoteTable (NoteId, SourceTable, SourceField, SourceTextField, KeyValue, Note) 
    SELECT N.NoteId, NKS.SourceTable, NKS.SourceField, NKS.SourceTextField, N.KeyValue, N.Note
        FROM Note AS N
            LEFT OUTER JOIN NoteKeySource AS NKS
                ON NKS.NoteKeySourceId = N.NoteKeySourceId

DECLARE @Context nvarchar(max)

DECLARE @Sql nvarchar(255);
DECLARE @SqlDecl nvarchar(255) = N'@Key int, @Ctx nvarchar(MAX) OUTPUT';


DECLARE @NoteId int, @KeyValue int, @SourceTable nvarchar(60), @SourceField nvarchar(60), @SourceTextField nvarchar(60)

DECLARE db_cursor CURSOR FOR SELECT SourceTable, SourceField, SourceTextField, KeyValue
        FROM #NoteTable WHERE SourceTable IS NOT NULL FOR UPDATE OF Context


OPEN db_cursor  
FETCH NEXT FROM db_cursor INTO @SourceTable, @SourceField, @SourceTextField, @KeyValue

WHILE @@FETCH_STATUS = 0  
BEGIN
    SET @Sql =  N'SELECT @Ctx = ' + @SourceTextField + ' FROM ' + @SourceTable + ' WHERE ' + @SourceField + '= @Key';

    EXECUTE sp_executesql @Sql, @SqlDecl, @Key = @KeyValue, @Ctx = @Context OUTPUT

    UPDATE #NoteTable SET Context=@Context WHERE CURRENT OF db_cursor

    FETCH NEXT FROM db_cursor INTO @SourceTable, @SourceField, @SourceTextField, @KeyValue
END

CLOSE db_cursor  
DEALLOCATE db_cursor 


SELECT NoteId, Context, Note from #NoteTable

DROP TABLE #NoteTable

RETURN 0

Calling spGetNotes returns:

1:Open:Opened the work order

2:Closed:Closed the work order

It works, but it is pretty ugly, and I would just like to know if there is a better/cleaner/more appropriate way to do this without using a cursor or a while loop. It is my understanding that dynamic SQL cannot be used in a subquery of normal SQL, so this was the best I could come up with.

I am relatively green when it comes to SQL, so if I am wrong about that, please enlighten me! Also, if there is a better overall approach to the general domain problem, that would also be welcome and appreciated :)

Was it helpful?

Solution

Create a view that provides a single EAV overlay across your source tables. Then join NoteKeySource to it.

CREATE VIEW AllSources AS (
  SELECT 
   'SourceTable1' AS [SourceTable], [SourceField], [SourceTextField]
  FROM SourceTable1
  UNPIVOT([SourceTextField] FOR [SourceField] IN
      ('Table1Field1','Table1Field2','Table1Field3','Table1Field4')
  ) p
  UNION ALL
  SELECT 
   'SourceTable2' AS [SourceTable], [SourceField], [SourceTextField]
  FROM SourceTable2
  UNPIVOT([SourceTextField] FOR [SourceField] IN
    ('Table2Field1','Table2Field2')
  ) p
  UNION ALL
  SELECT 
   'SourceTable3' AS [SourceTable], [SourceField], [SourceTextField]
  FROM SourceTable3
  UNPIVOT([SourceTextField] FOR [SourceField] IN
    ('Table3Field1','Table3Field2','Table3Field3')
  ) p
  --etc
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top