Вот пример, который мне удалось получить в SSMS:
-- Sample tables.
DECLARE @TableType TABLE ( [id] INT NOT NULL, [name] VARCHAR(100) NOT NULL );
DECLARE @ColumnType TABLE ( [id] INT NOT NULL, [name] VARCHAR(100) NOT NULL );
DECLARE @TableTypeAllowedObjectType TABLE (
[id] INT IDENTITY (1, 1) NOT NULL, [id_table_type] INT NOT NULL, [id_column_type] INT NOT NULL, [default_value] BIT NOT NULL
);
-- Sample data.
INSERT INTO @TableType (id, [name]) VALUES (1, 'TableType1'), (2, 'TableType2');
INSERT INTO @ColumnType (id, [name]) VALUES (1, 'ColumnType1'), (2, 'ColumnType2'), (3, 'ColumnType3');
INSERT INTO @TableTypeAllowedObjectType (id_table_type, id_column_type, default_value) VALUES (1, 1, 0), (1, 2, 0);
-- Variable for table type id.
DECLARE @TableTypeID INT = 1;
-- Query data based on table type id.
IF EXISTS( SELECT * FROM @TableTypeAllowedObjectType WHERE id_table_type = @TableTypeID )
SELECT * FROM @ColumnType AS ColumnType
WHERE EXISTS (
SELECT * FROM @TableTypeAllowedObjectType AS AllowedType
WHERE
ColumnType.id = AllowedType.id_column_type
AND AllowedType.id_table_type = @TableTypeID
)
ELSE
SELECT * FROM @ColumnType AS ColumnType
WHERE NOT EXISTS (
SELECT * FROM @TableTypeAllowedObjectType AS AllowedType
WHERE
ColumnType.id = AllowedType.id_column_type
AND AllowedType.id_table_type = @TableTypeID
)
@ TableTypeID = 1 возвращает:
+----+-------------+
| id | name |
+----+-------------+
| 1 | ColumnType1 |
| 2 | ColumnType2 |
+----+-------------+
@ TableTypeID = 2 возвращает:
+----+-------------+
| id | name |
+----+-------------+
| 1 | ColumnType1 |
| 2 | ColumnType2 |
| 3 | ColumnType3 |
+----+-------------+
Я полагаю, это то, что вы ищете?
ОБНОВЛЕНИЕ: Добавление функциональности в TVF.
Вы можете добавить эту функцию в табличную функцию, которую можно вызывать из вашего представления. Вот пример, основанный на том, какая информация доступна здесь:
-- Create TVF.
CREATE FUNCTION dbo.GetTableTypeColumns (
@TableTypeID INT
)
RETURNS @Results TABLE (
id INT, [name] VARCHAR(50)
)
AS
BEGIN
IF EXISTS( SELECT * FROM dbo.TableTypeAllowedObjectType WHERE id_table_type = @TableTypeID ) BEGIN
INSERT INTO @Results ( id, [name] )
SELECT * FROM dbo.ColumnType
WHERE EXISTS (
SELECT * FROM dbo.TableTypeAllowedObjectType AS AllowedType
WHERE
ColumnType.id = AllowedType.id_column_type
AND AllowedType.id_table_type = @TableTypeID
)
END ELSE BEGIN
INSERT INTO @Results ( id, [name] )
SELECT * FROM dbo.ColumnType
WHERE NOT EXISTS (
SELECT * FROM dbo.TableTypeAllowedObjectType AS AllowedType
WHERE
ColumnType.id = AllowedType.id_column_type
AND AllowedType.id_table_type = @TableTypeID
)
END
RETURN
END
GO
Как только вы создали TVF, вы можете назвать его так:
SELECT * FROM dbo.GetTableTypeColumns( 1 );
Returns
+----+-------------+
| id | name |
+----+-------------+
| 1 | ColumnType1 |
| 2 | ColumnType2 |
+----+-------------+
Или
SELECT * FROM dbo.GetTableTypeColumns( 2 );
Returns
+----+-------------+
| id | name |
+----+-------------+
| 1 | ColumnType1 |
| 2 | ColumnType2 |
| 3 | ColumnType3 |
+----+-------------+