Skip to main content

Posts

Showing posts with the label sql script

Drop foreign key relationships

This SQL query retrieves all foreign key constraints that reference the table in the current SQL Server database.     SELECT * FROM sys . foreign_keys WHERE referenced_object_id = object_id ( ' <TABLE_NAME> ' )      This SQL query finds all foreign key constraints that reference the   table and generates SQL statements to drop them. SELECT 'ALTER TABLE [' + OBJECT_SCHEMA_NAME ( parent_object_id ) + '].[' + OBJECT_NAME ( parent_object_id ) + '] DROP CONSTRAINT [' + name + ']' FROM sys . foreign_keys WHERE referenced_object_id = object_id ( '<TABLE_NAME>' )

Simple way to generate C# class from database table

 Use the below sql script to generate c# model corresponding to the table.   Set @TableName to the name of your table.   declare @TableName sysname = 'TableName' declare @Result varchar (max) = '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 'datetim...