Structure types in T-SQL

As I know SQL Server does not support struct types that contain other types, for example you cannot do something like this

create type User as(UserId int, Name varchar(10), Address varchar(255))

      

and then use it:

declare @user User
set @user.UserId = 10
...

      

Maybe someone knows some library / framework that helps emulate this behavior, for example there might be some set of functions that use the XML datatype to store values ​​in it and get them.

The problem I'm trying to solve is passing a lot of parameters between stored procedures. I have a procedure that takes almost 30 parameters than does some logic and feeds those parameters to another procedure, then the second procedure does some boolean and road parameters to the third procedure. Then the requirements change and new parameters have to be added to all procedures. With a data-type structure, it only converts to adding new fields to the data type. And another plus is readability.

+3


source to share


1 answer


Good day,

My first guess: you need a custom table type as mentioned in others. but you are wrong anyway and you can create new types containing other types. For example, I am using the "complex number" type that I create with SQLCLR.

You can see some simple code here: http://www.codeproject.com/Articles/15651/Creating-User-Defined-Data-Types-in-SQL-Server

  • This is not the code I am using :-) I just google it now.


To clarify, "complex number" is built from 2 prime decimal numbers, which means we use two types to create a custom data type :-)

  • I use other types as well, for example the other one is BIGBIGINT. I have a very large table that needs integers larger than BIGINT ... I have created another BIGBIGINT type that uses two BIGINT types.

Hope this is helpful :-)

+5


source







All Articles