It seems like you're looking to generate stored procedures dynamically based on input parameters @ModuleName and @Tables. The stored procedures generated include CRUD operations (Create, Read, Update, Delete) for the specified tables.
CREATE PROCEDURE
SP_Script_And_Model_Script
(
@ModuleName nvarchar(MAX) =
'FreeAndCourseModel',
@Tables nvarchar(MAX) =
'Student,Course'
)
AS
BEGIN
DECLARE @tableName
nvarchar(MAX) = '';
DECLARE @Result varchar(MAX)
= '';
DROP TABLE IF EXISTS
#Tables
SELECT ROW_NUMBER()
OVER(ORDER BY (SELECT NULL)) AS Row,* into #Tables from
dbo.Split(@Tables,',')
DECLARE @tot int,@cnt INT =
1;
SET @tot=(select count(*)
from #Tables)
BEGIN -- Class
WHILE @cnt <= @tot
BEGIN
SET @tableName = (select items from
#Tables where Row=@cnt)
BEGIN
SET @Result
= 'public class ' + @TableName + '
{'
SELECT @Result = @Result + '
public ' + ColumnType + NullableSign + ' ' +
ColumnName + ' { get; set; }'
FROM
(
SELECT
replace(col.name, ' ', '_')
ColumnName,
column_id ColumnId,
case typ.name
when 'bigint' then 'long'
when 'binary' then 'byte[]'
when 'bit' then 'bool'
when 'char' then 'string'
when 'date' then 'DateTime'
when 'datetime' then 'DateTime'
when 'datetime2' then 'DateTime'
when 'datetimeoffset' then
'DateTimeOffset'
when 'decimal' then 'decimal'
when 'float' then 'float'
when 'image' then 'byte[]'
when 'int' then 'int'
when 'money' then 'decimal'
when 'nchar' then 'char'
when 'ntext' then 'string'
when 'numeric' then 'decimal'
when 'nvarchar' then 'string'
when 'real' then 'double'
when 'smalldatetime' then 'DateTime'
when 'smallint' then 'short'
when 'smallmoney' then 'decimal'
when 'text' then 'string'
when 'time' then 'TimeSpan'
when 'timestamp' then 'DateTime'
when 'tinyint' then 'byte'
when 'uniqueidentifier' then 'Guid'
when 'varbinary' then 'byte[]'
when 'varchar' then 'string'
else 'UNKNOWN_' + typ.name
end ColumnType,
case
when col.is_nullable = 1 and typ.name in
('bigint', 'bit', 'date', 'datetime', 'datetime2', 'datetimeoffset', 'decimal',
'float', 'int', 'money', 'numeric', 'real', 'smalldatetime', 'smallint',
'smallmoney', 'time', 'tinyint', 'uniqueidentifier')
then '?'
else ''
end NullableSign
FROM sys.columns col
join
sys.types typ on
col.system_type_id = typ.system_type_id
AND col.user_type_id = typ.user_type_id
WHERE object_id = object_id(@TableName)
) t
ORDER BY ColumnId
SET @Result = @Result + '
}'
print cast(@Result as ntext)
END
SET @cnt=@cnt+1
END
END
BEGIN -- Insert SP
SET @cnt = 1;
SET @Result = '
CREATE OR ALTER PROCEDURE '+@ModuleName+'_Insert
@strJSON nvarchar(max) = ''''
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON
BEGIN TRY
BEGIN TRANSACTION ';
WHILE @cnt <= @tot
BEGIN
SET @tableName = (SELECT
items FROM #Tables WHERE Row=@cnt)
Begin -- Tables
SET @Result = @Result + '
DROP TABLE IF EXISTS
#'+@tableName+'Model
SELECT *
INTO #'+@tableName+'Model
FROM
OPENJSON (@strJSON, ''$.'+@tableName+''')
WITH
(';
SELECT @Result = @Result + '
' + ColumnName + ' ' + FullDataType + ' ' +
'''$.'+ ColumnName +''',' FROM (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))),
1, 1) = ',' THEN 1 ELSE 0 END,''))
SET @Result
= @Result + '
)'
set @Result = @Result + '
INSERT INTO ' + @tableName + '(';
SELECT @Result = @Result + ColumnName + ','
FROM (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN ('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN
SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))), 1, 1) = ',' THEN 1 ELSE 0
END,''))
SET @Result = @Result + ')';
SET @Result = @Result + '
SELECT ';
SELECT @Result = @Result + ColumnName + ','
FROM (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))),
1, 1) = ',' THEN 1 ELSE 0 END,''))
SET @Result = @Result + ' from #'+@tableName+'Model';
END
SET @cnt=@cnt+1
END
SET @Result = @Result +
'
COMMIT TRANSACTION
SELECT '''' AS ErrorMessage,1 AS
ResultType
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
DECLARE @ErrorMsg nvarchar(max) =
ERROR_MESSAGE()+ ''Line No : ''+CAST(ERROR_LINE() AS VARCHAR(50));
INSERT INTO
Error_Common_SingleTransaction
(Id,SP_Name,RequestJson,ErrorMsg,CreatedDate)
VALUES (newid(),'''+ @ModuleName
+'_Insert'',@strJSON,@ErrorMsg,GETDATE())
SELECT @ErrorMsg AS ErrorMessage,0 AS
ResultType
END CATCH
END';
print cast(@Result as ntext)
END
BEGIN -- Update SP
SET @cnt = 1;
SET @Result = '
CREATE OR ALTER PROCEDURE '+@ModuleName+'_Update
@strJSON nvarchar(max) = ''''
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON
BEGIN TRY
BEGIN TRANSACTION ';
WHILE @cnt <= @tot
BEGIN
SET @tableName = (SELECT
items FROM #Tables WHERE Row=@cnt)
BEGIN -- Insert
SET @Result = @Result + '
DROP TABLE IF EXISTS
#'+@tableName+'Model
SELECT *
INTO #'+@tableName+'Model
FROM
OPENJSON (@strJSON,
''$.'+@tableName+''')
WITH
(';
SELECT @Result = @Result + '
' + ColumnName + ' ' + FullDataType + ' ' +
'''$.'+ ColumnName +''',' from (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN
SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))), 1, 1) = ',' THEN 1 ELSE 0
END,''))
SET @Result
= @Result + '
)'
SET @Result = @Result + '
INSERT INTO ' + @tableName + '(';
SELECT @Result = @Result + ColumnName + ','
from (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+ CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))),
1, 1) = ',' THEN 1 ELSE 0 END,''))
SET @Result = @Result + ')';
SET @Result = @Result + '
SELECT ';
SELECT @Result = @Result + ColumnName + ','
from (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+ CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN
SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))), 1, 1) = ',' THEN 1 ELSE 0
END,''))
set @Result = @Result + ' from #'+@tableName+'Model';
END
Begin -- Update
SET @Result = @Result + '
DROP TABLE IF EXISTS
#'+@tableName+'Model_Update
SELECT *
INTO #'+@tableName+'Model
FROM
OPENJSON (@strJSON,
''$.'+@tableName+'Model_Update'')
WITH
(';
SELECT @Result = @Result + '
' + ColumnName + ' ' + FullDataType + ' ' +
'''$.'+ ColumnName +''',' from (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))),
1, 1) = ',' THEN 1 ELSE 0 END,''))
SET @Result
= @Result + '
)'
SET @Result = @Result + '
UPDATE X SET ';
SELECT @Result = @Result +'
X.'+ ColumnName +'=Y.'+ ColumnName + ','
from (
SELECT
c.name 'ColumnName',
t.name,
t.name +
CASE WHEN t.name IN ('char',
'varchar','nchar','nvarchar') THEN '('+
CASE WHEN c.max_length=-1 THEN
'MAX'
ELSE CONVERT(VARCHAR(4),
CASE WHEN t.name IN
('nchar','nvarchar')
THEN c.max_length/2 ELSE c.max_length END )
END +')'
WHEN t.name IN
('decimal','numeric')
THEN '('+
CONVERT(VARCHAR(4),c.precision)+','
+
CONVERT(VARCHAR(4),c.Scale)+')'
ELSE '' END
as "FullDataType",
c.max_length 'Max Length in Bytes',
c.precision ,
c.scale ,
c.is_nullable
--,ISNULL(i.is_primary_key, 0) 'Primary
Key'
FROM
sys.columns c
INNER JOIN
sys.types t ON c.user_type_id =
t.user_type_id
--LEFT OUTER JOIN
--
sys.index_columns ic ON ic.object_id = c.object_id AND ic.column_id =
c.column_id
--LEFT OUTER JOIN
--
sys.indexes i ON ic.object_id = i.object_id AND ic.index_id =
i.index_id
WHERE
c.object_id = OBJECT_ID(@tableName)
) as t
SELECT @Result =
REVERSE(STUFF(REVERSE(LTRIM(RTRIM(@Result))), 1,CASE WHEN
SUBSTRING((REVERSE(LTRIM(RTRIM(@Result)))), 1, 1) = ',' THEN 1 ELSE 0
END,''))
SET @Result = @Result + '
FROM '+@tableName+' X
JOIN #'+@tableName+'Model_Update Y ON X.Id =
Y.Id';
END
SET @cnt=@cnt+1
END
SET @Result = @Result +
'
COMMIT TRANSACTION
SELECT '''' AS ErrorMessage,1 AS
ResultType
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
DECLARE @ErrorMsg nvarchar(max) =
ERROR_MESSAGE()+ ''Line No : ''+CAST(ERROR_LINE() AS VARCHAR(50));
INSERT INTO
Error_Common_SingleTransaction
(Id,SP_Name,RequestJson,ErrorMsg,CreatedDate)
VALUES (newid(),'''+ @ModuleName
+'_Update'',@strJSON,@ErrorMsg,GETDATE())
END CATCH
END';
print cast(@Result as ntext)
END
BEGIN -- Get By Id SP
SET @cnt = 1;
SET @Result = '
CREATE OR ALTER PROCEDURE '+@ModuleName+'_GetById
@Id uniqueidentifier = null
AS
BEGIN';
WHILE @cnt <= @tot
BEGIN
SET @tableName = (SELECT
items FROM #Tables WHERE Row=@cnt)
BEGIN -- Tables
SET @Result = @Result + '
SELECT * FROM '+@tableName+'(nolock) WHERE
Id = @Id'
END
SET @cnt=@cnt+1
END
SET @Result = @Result +
'
END';
print cast(@Result as ntext)
END
BEGIN -- Delete SP
SET @cnt = 1;
SET @Result = '
CREATE OR ALTER PROCEDURE '+@ModuleName+'_Delete
@Ids nvarchar(max)
AS
BEGIN
SET NOCOUNT ON;
SET XACT_ABORT ON
BEGIN TRY
BEGIN TRANSACTION ';
WHILE @cnt <= @tot
BEGIN
SET @tableName = (SELECT
items FROM #Tables WHERE Row=@cnt)
BEGIN -- Tables
SET @Result = @Result + '
UPDATE '+@tableName+' SET IsDeleted = 1
WHERE Id in (SELECT cast(Items as uniqueidentifier) FROM
dbo.Split(@Ids,'',''))'
END
SET @cnt=@cnt+1
END
SET @Result = @Result +
'
COMMIT TRANSACTION
SELECT '''' AS ErrorMessage,1 AS
ResultType
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION
END
SELECT ERROR_MESSAGE()+ ''Line No :
''+CAST(ERROR_LINE() AS VARCHAR(50)) AS ErrorMessage,0 AS ResultType
END CATCH
END';
PRINT cast(@Result as ntext)
END
END