In Oracle's PL-SQL, you can declare a variable and define its type based on a table column:
declare var1 table.column%TYPE;
Is it possible to do something similar in MS SQL Server?
The %TYPE attribute provides the datatype of a variable or table column. This is particularly useful when declaring variables that will hold values of a table column.
The %TYPE attribute, used in PL/SQL variable and parameter declarations, is supported by the data server. Use of this attribute ensures that type compatibility between table columns and PL/SQL variables is maintained.
Declaring variables as the type table_name %ROWTYPE is a convenient way to transfer data between database tables and PL/SQL.
%ROWTYPE. Specifies that the record field data types are to be derived from the column data types that are associated with the identified table or view. Record fields do not inherit any other column attributes, such as, for example, the nullability attribute.
No you can't do this. The closest equivalent is User-Defined Data Types
. This will give you a layer of abstraction that may help, but it is not the same as deriving a type from a column.
It may skirt the real issue, but you can "cheat" a little bit by
Select * INTO #tmp From MyTable Where 1 = 0
Will automatically create a temp table with all columns with correct data types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With