Couple of Useful Queries - Identity Insert Without Identity Column & View Schema Definition
Identity Insert Without Identity Column
Problem Statement - There is a source table and a target table. The structure is almost similar.
The ask is to insert data from the source table to the target table.
The target table has an additional column of int type which could be used as a row identifier, however, it's not configured to have auto identity insert and you cannot redefine/recreate the target table.
Solution - Used Row_Number window function by partitioning it with a constant value. Please see below
Let me know if you guys can think of some other way!
Fetch the schema details of a view
Got a rarer requirement to list out the columns returned from a view along with its data type.
Here is the query
This comment has been removed by the author.
ReplyDeleteselect
ReplyDeleteschema_name(v.schema_id) schema_name,
object_name(c.object_id) as view_name,
c.column_id,
c.name as column_name,
type_name (c.user_type_id) as data_type,
c.max_length,
c.precision
from sys.columns c
join sys.views v
on v.object_id = c.object_id
where object_name(c.object_id) ='VIEWNAME'
order by schema_name