Last active
June 3, 2025 14:00
-
-
Save tcartwright/29b8a3cd5a46a94d5c3dfe36a1f48b4c to your computer and use it in GitHub Desktop.
SQL: Generate Dapper Params from SQL Server Objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @procedure_name sysname = 'dbo.procedure_name' | |
SELECT | |
OBJECT_SCHEMA_NAME([p].[object_id]) AS [schema], | |
OBJECT_NAME([p].[object_id]) AS [procedure_name], | |
[p].[name] AS [parameter_name], | |
CONCAT('sqlParams.Add("', [p].[name] ,'", ', [p].[name], ', ', [fn].db_type, ');') | |
FROM sys.[parameters] AS [p] | |
INNER JOIN sys.[types] AS [t] | |
ON [p].[system_type_id] = [t].[system_type_id] | |
AND [p].[user_type_id] = [t].[user_type_id] | |
CROSS APPLY ( | |
SELECT | |
CASE t.name | |
-- String types with length | |
WHEN 'char' THEN CONCAT('DbType.AnsiStringFixedLength, size: ', [p].max_length) | |
WHEN 'varchar' THEN | |
CASE WHEN [p].max_length = -1 THEN 'DbType.AnsiString, size: -1' -- MAX | |
ELSE CONCAT('DbType.AnsiString, size: ', [p].max_length) | |
END | |
WHEN 'nchar' THEN CONCAT('DbType.StringFixedLength, size: ', [p].max_length/2) | |
WHEN 'nvarchar' THEN | |
CASE WHEN [p].max_length = -1 THEN 'DbType.String, size: -1' -- MAX | |
ELSE CONCAT('DbType.String, size: ', [p].max_length/2) | |
END | |
WHEN 'text' THEN 'DbType.AnsiString, size: -1' | |
WHEN 'ntext' THEN 'DbType.String, size: -1' | |
-- Numeric types with precision and scale | |
WHEN 'decimal' THEN CONCAT('DbType.Decimal, precision: ', [p].[precision], ', scale: ', [p].[scale]) | |
WHEN 'numeric' THEN CONCAT('DbType.Decimal, precision: ', [p].[precision], ', scale: ', [p].[scale]) | |
-- Integer types | |
WHEN 'bit' THEN 'DbType.Boolean' | |
WHEN 'tinyint' THEN 'DbType.Byte' | |
WHEN 'smallint' THEN 'DbType.Int16' | |
WHEN 'int' THEN 'DbType.Int32' | |
WHEN 'bigint' THEN 'DbType.Int64' | |
-- Float/Real types | |
WHEN 'real' THEN 'DbType.Single' | |
WHEN 'float' THEN 'DbType.Double' | |
WHEN 'money' THEN 'DbType.Currency' | |
WHEN 'smallmoney' THEN 'DbType.Currency' | |
-- Date/Time types | |
WHEN 'date' THEN 'DbType.Date' | |
WHEN 'smalldatetime' THEN 'DbType.DateTime' | |
WHEN 'datetime' THEN 'DbType.DateTime' | |
WHEN 'datetime2' THEN 'DbType.DateTime2' | |
WHEN 'datetimeoffset' THEN 'DbType.DateTimeOffset' | |
WHEN 'time' THEN 'DbType.Time' | |
-- Binary types | |
WHEN 'binary' THEN CONCAT('DbType.Binary, size: ', [p].max_length) | |
WHEN 'varbinary' THEN | |
CASE WHEN [p].max_length = -1 THEN 'DbType.Binary, size: -1' -- MAX | |
ELSE CONCAT('DbType.Binary, size: ', [p].max_length) | |
END | |
WHEN 'image' THEN 'DbType.Binary, size: -1' | |
-- Other types | |
WHEN 'uniqueidentifier' THEN 'DbType.Guid' | |
WHEN 'xml' THEN 'DbType.Xml' | |
WHEN 'sql_variant' THEN 'DbType.Object' | |
ELSE 'DbType.Object' -- Default fallback | |
END AS [db_type] | |
) AS [fn] | |
WHERE [p].[object_id] = OBJECT_ID(@procedure_name) | |
ORDER BY [p].[parameter_id] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DECLARE @sql_query NVARCHAR(MAX) = N' | |
SELECT | |
Id, | |
Name, | |
CreatedDate, | |
Amount | |
FROM dbo.YourViewOrComplexQuery | |
'; | |
-- Use metadata function to get column info | |
SELECT | |
col.name AS column_name, | |
CONCAT('sqlParams.Add("@', col.name ,'", ', col.name, ', ', | |
CASE | |
WHEN system_type_name LIKE 'char%' THEN CONCAT('DbType.AnsiStringFixedLength, size: ', max_length) | |
WHEN system_type_name LIKE 'varchar%' THEN | |
CASE WHEN max_length = -1 THEN 'DbType.AnsiString, size: -1' | |
ELSE CONCAT('DbType.AnsiString, size: ', max_length) END | |
WHEN system_type_name LIKE 'nchar%' THEN CONCAT('DbType.StringFixedLength, size: ', max_length/2) | |
WHEN system_type_name LIKE 'nvarchar%' THEN | |
CASE WHEN max_length = -1 THEN 'DbType.String, size: -1' | |
ELSE CONCAT('DbType.String, size: ', max_length/2) END | |
WHEN system_type_name IN ('text') THEN 'DbType.AnsiString, size: -1' | |
WHEN system_type_name IN ('ntext') THEN 'DbType.String, size: -1' | |
WHEN system_type_name IN ('decimal', 'numeric') THEN CONCAT('DbType.Decimal, precision: ', precision, ', scale: ', scale) | |
WHEN system_type_name = 'bit' THEN 'DbType.Boolean' | |
WHEN system_type_name = 'tinyint' THEN 'DbType.Byte' | |
WHEN system_type_name = 'smallint' THEN 'DbType.Int16' | |
WHEN system_type_name = 'int' THEN 'DbType.Int32' | |
WHEN system_type_name = 'bigint' THEN 'DbType.Int64' | |
WHEN system_type_name = 'real' THEN 'DbType.Single' | |
WHEN system_type_name = 'float' THEN 'DbType.Double' | |
WHEN system_type_name IN ('money', 'smallmoney') THEN 'DbType.Currency' | |
WHEN system_type_name IN ('date') THEN 'DbType.Date' | |
WHEN system_type_name IN ('datetime', 'smalldatetime') THEN 'DbType.DateTime' | |
WHEN system_type_name IN ('datetime2') THEN 'DbType.DateTime2' | |
WHEN system_type_name = 'datetimeoffset' THEN 'DbType.DateTimeOffset' | |
WHEN system_type_name = 'time' THEN 'DbType.Time' | |
WHEN system_type_name = 'uniqueidentifier' THEN 'DbType.Guid' | |
WHEN system_type_name = 'binary' THEN CONCAT('DbType.Binary, size: ', max_length) | |
WHEN system_type_name = 'varbinary' THEN | |
CASE WHEN max_length = -1 THEN 'DbType.Binary, size: -1' | |
ELSE CONCAT('DbType.Binary, size: ', max_length) END | |
WHEN system_type_name = 'image' THEN 'DbType.Binary, size: -1' | |
WHEN system_type_name = 'xml' THEN 'DbType.Xml' | |
WHEN system_type_name = 'sql_variant' THEN 'DbType.Object' | |
ELSE 'DbType.Object' | |
END | |
, ');') AS sql_param_line | |
FROM sys.dm_exec_describe_first_result_set(@sql_query, NULL, 1) AS col | |
WHERE col.is_hidden = 0 | |
ORDER BY col.column_ordinal; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could have written the data type conversion as a function, but I did not want to have to create a function if not needed.