Я создаю табличные функции-пустышки.
Чтобы создать все поля, я просто сгенерировал их как
SELECT
CAST(NULL AS <datatype>) AS AliasColumn1
,CAST(NULL AS <datatype>) AS AliasColumn2
,...
например,
SELECT
CAST(NULL AS int) AS Field1
,CAST(NULL AS nvarchar(MAX)) AS Field2
Таким образом, мне не нужно правильно получать зависимости, когда ясоздать все функции / таблицы / представления / и т. д. Теперь это работает нормально, за исключением того, что возвращает неправильную обнуляемость. Поэтому я пытаюсь сгенерировать допустимое фиктивное поле как ненулевое значение . Чтобы сделать это, я просто заключаю NULL в ISNULL.
Это прекрасно работает почти для всех типов данных, кроме географии, геометрии, изображений и XML. Можно ли вообще создать необнуляемое geography | geometry | image | XML-поле в select / view?
Независимо от того, что я пробовал, география, геометрия, изображение, xml остаются на NULLABLE: да... Это то, что я делал до сих пор
DROP VIEW IF EXISTS dbo.TestTypes;
GO
CREATE VIEW dbo.TestTypes
AS
SELECT
CURRENT_TIMESTAMP AS foo
/*
,CAST(NULL AS int) AS Field1
,ISNULL(CAST(NULL AS int), '123') AS Field2
,ISNULL(CAST(NULL AS int), 'A') AS Field2_Error
,ISNULL(CAST(NULL AS decimal(5,2)), '123.456') AS Field3
,ISNULL(CAST(NULL AS bit), '123') AS Field4
,ISNULL(CAST(NULL AS float), '123') AS Field5
,ISNULL(CAST(NULL AS char), '123') AS Field6
,ISNULL(CAST(NULL AS text), '123') AS Field7
*/
/*
,ISNULL(CAST(NULL AS bigint), '20190101') AS Field1
,ISNULL(CAST(NULL AS binary), 0) AS Field2
,ISNULL(CAST(NULL AS bit), '20190101') AS Field3
,ISNULL(CAST(NULL AS char), '20190101') AS Field4
,ISNULL(CAST(NULL AS date), '20190101') AS Field5
,ISNULL(CAST(NULL AS datetime), '20190101') AS Field6
,ISNULL(CAST(NULL AS datetime2), '20190101') AS Field7
,ISNULL(CAST(NULL AS datetimeoffset), '20190101') AS Field8
,ISNULL(CAST(NULL AS decimal), 0) AS Field9
,ISNULL(CAST(NULL AS float), '20190101') AS Field10
,ISNULL(CAST(NULL AS geography), 'POLYGON EMPTY') AS Field11
,ISNULL(CAST(NULL AS geometry), 'POLYGON EMPTY') AS Field12
,ISNULL(CAST(NULL AS hierarchyid), CAST('/0/' AS hierarchyid)) AS Field13
,ISNULL(CAST(NULL AS image), '20190101') AS Field14
,ISNULL(CAST(NULL AS int), '20190101') AS Field15
,ISNULL(CAST(NULL AS money), '20190101') AS Field16
,ISNULL(CAST(NULL AS nchar), '20190101') AS Field17
,ISNULL(CAST(NULL AS ntext), '20190101') AS Field18
,ISNULL(CAST(NULL AS numeric), 0) AS Field19
,ISNULL(CAST(NULL AS nvarchar), '20190101') AS Field20
,ISNULL(CAST(NULL AS real), '20190101') AS Field21
,ISNULL(CAST(NULL AS smalldatetime), '20190101') AS Field22
,ISNULL(CAST(NULL AS smallint), 0) AS Field23
,ISNULL(CAST(NULL AS smallmoney), 0) AS Field24
,ISNULL(CAST(NULL AS sql_variant), '20190101') AS Field25
,ISNULL(CAST(NULL AS sysname), '20190101') AS Field26
,ISNULL(CAST(NULL AS text), '20190101') AS Field27
,ISNULL(CAST(NULL AS time), '20190101') AS Field28
,ISNULL(CAST(NULL AS timestamp), 0) AS Field29
,ISNULL(CAST(NULL AS tinyint), 0) AS Field30
,ISNULL(CAST(NULL AS uniqueidentifier), '00000000-0000-0000-0000-000000000000') AS Field31
,ISNULL(CAST(NULL AS varbinary), 0) AS Field32
,ISNULL(CAST(NULL AS varchar), '20190101') AS Field33
,ISNULL(CAST(NULL AS xml), '20190101') AS Field34
*/
,ISNULL(CAST(NULL AS geography), 'POLYGON EMPTY') AS Field11
,ISNULL(CAST(NULL AS geography), 'POLYGON((1 1, 3 3, 3 1, 1 1))') AS Field11a
,CAST('POLYGON EMPTY' AS geography) AS Field11B
,ISNULL(CAST(NULL AS geometry), 'POLYGON EMPTY') AS Field12
,ISNULL(CAST(NULL AS geometry), 'POLYGON((1 1, 3 3, 3 1, 1 1))') AS Field12a
,CAST('POLYGON EMPTY' AS geometry) AS Field12B
,ISNULL(CAST(NULL AS image), '20190101') AS Field14
,ISNULL(CAST(NULL AS image), 0x123567AB) AS Field14A
,CAST(0x123567AB AS image) AS Field14B
,ISNULL(CAST(NULL AS xml), CAST('<h1>Test</h1>' AS xml)) AS Field34
, CAST('<?xml version="1.0" encoding="utf-8"?><html></html>' AS xml) AS Field34A
GO
--geography
--geometry
--image
--xml
SELECT * FROM information_schema.columns WHERE table_name = 'TestTypes'
-- SELECT * FROM information_schema.columns WHERE table_name = 'Table_1'
/*
;WITH CTE AS
(
SELECT TOP 999999999 name, CAST(ROW_NUMBER() OVER (ORDER BY name) AS varchar(20)) AS rn
FROM sys.types WHERE is_user_defined = 0 ORDER BY name
)
SELECT
name
,
CASE
WHEN name = 'uniqueidentifier' THEN ' SELECT ISNULL(CAST(NULL AS uniqueidentifier), ''00000000-0000-0000-0000-000000000000'') AS Field' + rn
WHEN name = 'hierarchyid' THEN ' SELECT ISNULL(CAST(NULL AS ' + name + '), CAST(''/0/'' AS hierarchyid)) AS Field' + rn
WHEN name IN ('geography', 'geometry') THEN ' SELECT ISNULL(CAST(NULL AS ' + name + '), ''POLYGON EMPTY'') AS Field' + rn
WHEN name IN ('tinyint', 'smallmoney', 'smallint', 'numeric', 'decimal', 'binary', 'varbinary', 'timestamp') THEN ' SELECT ISNULL(CAST(NULL AS ' + name + '), 0) AS Field' + rn
ELSE ' SELECT ISNULL(CAST(NULL AS ' + name + '), ''20190101'') AS Field' + rn
END AS sql2
FROM CTE
*/