SqlMetaData.Name seems irrelevant when passing to user defined Table Type via SqlDataRecordHow do I check if a SQL Server text column is empty?Hidden Features of SQL ServerT-SQL Conditional Order ByHow can I include a RowVerson column in a table valued parameter when calling a stored procedure?where condition in TSQL based on input parametersHow to fully qualify column names in user-defined table types?Get Common data from two different tableImplement more conditions on upsert (user defined table types) in SQL Server?Selecting column names where values are NULLSQL - SELECT first matching row column value and any subsequent row value as null for specific columns or tables

Why do cheap flights with a layover get more expensive when you split them up into separate flights?

Why is Chromosome 1 called Chromosome 1?

Which genus do I use for neutral expressions in German?

Did Apollo leave poop on the moon?

Is an "are" omitted in this sentence

Changing Row Keys into Normal Rows

Why can I log in to my facebook account with misspelled email/password

Examples of application problems of coordinate geometry in the complex plane?

Write The Shortest Program To Check If A Binary Tree Is Balanced

Getting matrices labels

How many years before enough atoms of your body are replaced to survive the sudden disappearance of the original body’s atoms?

What is the corner house number?

Is space radiation a risk for space film photography, and how is this prevented?

Why is power of a hypothesis test a concern when we can bootstrap any representative sample to make n approach infinity?

Not been paid even after reminding the Treasurer; what should I do?

How to realistically deal with a shield user?

Generate a random point outside a given rectangle within a map

Why does putting a dot after the URL remove login information?

I was contacted by a private bank overseas to get my inheritance

Ancients don't give a full level?

How to approach protecting my code as a research assistant? Should I be worried in the first place?

Should I take out a personal loan to pay off credit card debt?

Why am I not getting stuck in the loop

Can I enter Switzerland with only my London Driver's License?



SqlMetaData.Name seems irrelevant when passing to user defined Table Type via SqlDataRecord


How do I check if a SQL Server text column is empty?Hidden Features of SQL ServerT-SQL Conditional Order ByHow can I include a RowVerson column in a table valued parameter when calling a stored procedure?where condition in TSQL based on input parametersHow to fully qualify column names in user-defined table types?Get Common data from two different tableImplement more conditions on upsert (user defined table types) in SQL Server?Selecting column names where values are NULLSQL - SELECT first matching row column value and any subsequent row value as null for specific columns or tables






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have a stored procedure that accepts a user-defined Table Type as a parameter. In C# I use SqlDataRecord and SqlMetaData (and SqlParameter) to create a table parameter with my data and send that to the stored procedure.



This works fine when the columns (SqlMetaData) are in the same order as the column definitions within the actual Table Type sql-server. If they are NOT in the same order then the wrong columns will be matched up.



So what is the point of the SqlMetaData.Name property? It seems to be ignored (at least for this use case) and only the order of columns (and data) are considered.




Example...



I have a Table Type in SQL:



CREATE TYPE [dbo].[TestingTableParamType] AS TABLE(
[FName] VARCHAR(50) NULL,
[Numb] INT NULL,
[LName] VARCHAR(50) NULL
);


I have my stored procedure:



CREATE PROCEDURE spTEST_TableParam
@input TestingTableParamType READONLY
AS
SELECT FName, Numb, LName FROM @input


And I have my test data generator method which yields three rows of test data with the appropriate column types in the appropriate order (to match the TestingTableParamType definition). Notice, however, that I use blank strings as names! I tried with nonsense strings and that worked fine also (nulls will cause an exception):



private IEnumerable<SqlDataRecord> genTestSqlDataRecords()

SqlDataRecord record = new SqlDataRecord(
new SqlMetaData("", SqlDbType.VarChar, 50),
new SqlMetaData("", SqlDbType.Int),
new SqlMetaData("", SqlDbType.VarChar, 50));

record.SetValues("fn1", 101, "ln1");
yield return record;
record.SetValues("fn2", 102, "ln2");
yield return record;
record.SetValues("fn3", 103, "ln3");
yield return record;



One could try specifying the column names and re-ordering the SqlMetaData columns but it will simply use the column order to link up to the Table Type not the column names.



There is some more code that does the database call itself, here is a relevant snippet:



...
SqlParameter param = new SqlParameter("@input", SqlDbType.Structured);
param.Direction = ParameterDirection.Input;
param.TypeName = "TestingTableParamType";
param.Value = genTestSqlDataRecords();

dbCommand.Parameters.Add(param);

conn.Open();

var dataReader = dbCommand.ExecuteReader();
...


So, it seems that the SqlMetaData (column) Name property is irrelevant, at least for this use case. This creates for misleading code - in that it appears like you could reorder the columns (and data) and it should match them up by their names, but that doesn't happen!



Additionally all the examples I've seen of using SqlDataRecord & SqlMetaData, to pass Table Type params, provide names for their columns as if it is necessary.



What have I missed in all of this?
Thank you kindly.










share|improve this question
































    1















    I have a stored procedure that accepts a user-defined Table Type as a parameter. In C# I use SqlDataRecord and SqlMetaData (and SqlParameter) to create a table parameter with my data and send that to the stored procedure.



    This works fine when the columns (SqlMetaData) are in the same order as the column definitions within the actual Table Type sql-server. If they are NOT in the same order then the wrong columns will be matched up.



    So what is the point of the SqlMetaData.Name property? It seems to be ignored (at least for this use case) and only the order of columns (and data) are considered.




    Example...



    I have a Table Type in SQL:



    CREATE TYPE [dbo].[TestingTableParamType] AS TABLE(
    [FName] VARCHAR(50) NULL,
    [Numb] INT NULL,
    [LName] VARCHAR(50) NULL
    );


    I have my stored procedure:



    CREATE PROCEDURE spTEST_TableParam
    @input TestingTableParamType READONLY
    AS
    SELECT FName, Numb, LName FROM @input


    And I have my test data generator method which yields three rows of test data with the appropriate column types in the appropriate order (to match the TestingTableParamType definition). Notice, however, that I use blank strings as names! I tried with nonsense strings and that worked fine also (nulls will cause an exception):



    private IEnumerable<SqlDataRecord> genTestSqlDataRecords()

    SqlDataRecord record = new SqlDataRecord(
    new SqlMetaData("", SqlDbType.VarChar, 50),
    new SqlMetaData("", SqlDbType.Int),
    new SqlMetaData("", SqlDbType.VarChar, 50));

    record.SetValues("fn1", 101, "ln1");
    yield return record;
    record.SetValues("fn2", 102, "ln2");
    yield return record;
    record.SetValues("fn3", 103, "ln3");
    yield return record;



    One could try specifying the column names and re-ordering the SqlMetaData columns but it will simply use the column order to link up to the Table Type not the column names.



    There is some more code that does the database call itself, here is a relevant snippet:



    ...
    SqlParameter param = new SqlParameter("@input", SqlDbType.Structured);
    param.Direction = ParameterDirection.Input;
    param.TypeName = "TestingTableParamType";
    param.Value = genTestSqlDataRecords();

    dbCommand.Parameters.Add(param);

    conn.Open();

    var dataReader = dbCommand.ExecuteReader();
    ...


    So, it seems that the SqlMetaData (column) Name property is irrelevant, at least for this use case. This creates for misleading code - in that it appears like you could reorder the columns (and data) and it should match them up by their names, but that doesn't happen!



    Additionally all the examples I've seen of using SqlDataRecord & SqlMetaData, to pass Table Type params, provide names for their columns as if it is necessary.



    What have I missed in all of this?
    Thank you kindly.










    share|improve this question




























      1












      1








      1








      I have a stored procedure that accepts a user-defined Table Type as a parameter. In C# I use SqlDataRecord and SqlMetaData (and SqlParameter) to create a table parameter with my data and send that to the stored procedure.



      This works fine when the columns (SqlMetaData) are in the same order as the column definitions within the actual Table Type sql-server. If they are NOT in the same order then the wrong columns will be matched up.



      So what is the point of the SqlMetaData.Name property? It seems to be ignored (at least for this use case) and only the order of columns (and data) are considered.




      Example...



      I have a Table Type in SQL:



      CREATE TYPE [dbo].[TestingTableParamType] AS TABLE(
      [FName] VARCHAR(50) NULL,
      [Numb] INT NULL,
      [LName] VARCHAR(50) NULL
      );


      I have my stored procedure:



      CREATE PROCEDURE spTEST_TableParam
      @input TestingTableParamType READONLY
      AS
      SELECT FName, Numb, LName FROM @input


      And I have my test data generator method which yields three rows of test data with the appropriate column types in the appropriate order (to match the TestingTableParamType definition). Notice, however, that I use blank strings as names! I tried with nonsense strings and that worked fine also (nulls will cause an exception):



      private IEnumerable<SqlDataRecord> genTestSqlDataRecords()

      SqlDataRecord record = new SqlDataRecord(
      new SqlMetaData("", SqlDbType.VarChar, 50),
      new SqlMetaData("", SqlDbType.Int),
      new SqlMetaData("", SqlDbType.VarChar, 50));

      record.SetValues("fn1", 101, "ln1");
      yield return record;
      record.SetValues("fn2", 102, "ln2");
      yield return record;
      record.SetValues("fn3", 103, "ln3");
      yield return record;



      One could try specifying the column names and re-ordering the SqlMetaData columns but it will simply use the column order to link up to the Table Type not the column names.



      There is some more code that does the database call itself, here is a relevant snippet:



      ...
      SqlParameter param = new SqlParameter("@input", SqlDbType.Structured);
      param.Direction = ParameterDirection.Input;
      param.TypeName = "TestingTableParamType";
      param.Value = genTestSqlDataRecords();

      dbCommand.Parameters.Add(param);

      conn.Open();

      var dataReader = dbCommand.ExecuteReader();
      ...


      So, it seems that the SqlMetaData (column) Name property is irrelevant, at least for this use case. This creates for misleading code - in that it appears like you could reorder the columns (and data) and it should match them up by their names, but that doesn't happen!



      Additionally all the examples I've seen of using SqlDataRecord & SqlMetaData, to pass Table Type params, provide names for their columns as if it is necessary.



      What have I missed in all of this?
      Thank you kindly.










      share|improve this question
















      I have a stored procedure that accepts a user-defined Table Type as a parameter. In C# I use SqlDataRecord and SqlMetaData (and SqlParameter) to create a table parameter with my data and send that to the stored procedure.



      This works fine when the columns (SqlMetaData) are in the same order as the column definitions within the actual Table Type sql-server. If they are NOT in the same order then the wrong columns will be matched up.



      So what is the point of the SqlMetaData.Name property? It seems to be ignored (at least for this use case) and only the order of columns (and data) are considered.




      Example...



      I have a Table Type in SQL:



      CREATE TYPE [dbo].[TestingTableParamType] AS TABLE(
      [FName] VARCHAR(50) NULL,
      [Numb] INT NULL,
      [LName] VARCHAR(50) NULL
      );


      I have my stored procedure:



      CREATE PROCEDURE spTEST_TableParam
      @input TestingTableParamType READONLY
      AS
      SELECT FName, Numb, LName FROM @input


      And I have my test data generator method which yields three rows of test data with the appropriate column types in the appropriate order (to match the TestingTableParamType definition). Notice, however, that I use blank strings as names! I tried with nonsense strings and that worked fine also (nulls will cause an exception):



      private IEnumerable<SqlDataRecord> genTestSqlDataRecords()

      SqlDataRecord record = new SqlDataRecord(
      new SqlMetaData("", SqlDbType.VarChar, 50),
      new SqlMetaData("", SqlDbType.Int),
      new SqlMetaData("", SqlDbType.VarChar, 50));

      record.SetValues("fn1", 101, "ln1");
      yield return record;
      record.SetValues("fn2", 102, "ln2");
      yield return record;
      record.SetValues("fn3", 103, "ln3");
      yield return record;



      One could try specifying the column names and re-ordering the SqlMetaData columns but it will simply use the column order to link up to the Table Type not the column names.



      There is some more code that does the database call itself, here is a relevant snippet:



      ...
      SqlParameter param = new SqlParameter("@input", SqlDbType.Structured);
      param.Direction = ParameterDirection.Input;
      param.TypeName = "TestingTableParamType";
      param.Value = genTestSqlDataRecords();

      dbCommand.Parameters.Add(param);

      conn.Open();

      var dataReader = dbCommand.ExecuteReader();
      ...


      So, it seems that the SqlMetaData (column) Name property is irrelevant, at least for this use case. This creates for misleading code - in that it appears like you could reorder the columns (and data) and it should match them up by their names, but that doesn't happen!



      Additionally all the examples I've seen of using SqlDataRecord & SqlMetaData, to pass Table Type params, provide names for their columns as if it is necessary.



      What have I missed in all of this?
      Thank you kindly.







      c# sql-server tsql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 4:35







      devlop

















      asked Mar 27 at 4:02









      devlopdevlop

      7395 silver badges17 bronze badges




      7395 silver badges17 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0















          what is the point of the SqlMetaData.Name property?




          None whatsoever here. But for a CLR stored procedure streaming a result set via SendResultsStart and related methods, or Send(SqlDataRecord), it's vital because before those methods are called, SQL Server has no idea what the "shape"1 of the result set is, and it uses this metadata to understand it (and to be able to forward this metadata back to client applications also).




          1I often use shape when talking about result sets. It's the number of columns, their names and their data types.






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369614%2fsqlmetadata-name-seems-irrelevant-when-passing-to-user-defined-table-type-via-sq%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0















            what is the point of the SqlMetaData.Name property?




            None whatsoever here. But for a CLR stored procedure streaming a result set via SendResultsStart and related methods, or Send(SqlDataRecord), it's vital because before those methods are called, SQL Server has no idea what the "shape"1 of the result set is, and it uses this metadata to understand it (and to be able to forward this metadata back to client applications also).




            1I often use shape when talking about result sets. It's the number of columns, their names and their data types.






            share|improve this answer































              0















              what is the point of the SqlMetaData.Name property?




              None whatsoever here. But for a CLR stored procedure streaming a result set via SendResultsStart and related methods, or Send(SqlDataRecord), it's vital because before those methods are called, SQL Server has no idea what the "shape"1 of the result set is, and it uses this metadata to understand it (and to be able to forward this metadata back to client applications also).




              1I often use shape when talking about result sets. It's the number of columns, their names and their data types.






              share|improve this answer





























                0












                0








                0








                what is the point of the SqlMetaData.Name property?




                None whatsoever here. But for a CLR stored procedure streaming a result set via SendResultsStart and related methods, or Send(SqlDataRecord), it's vital because before those methods are called, SQL Server has no idea what the "shape"1 of the result set is, and it uses this metadata to understand it (and to be able to forward this metadata back to client applications also).




                1I often use shape when talking about result sets. It's the number of columns, their names and their data types.






                share|improve this answer
















                what is the point of the SqlMetaData.Name property?




                None whatsoever here. But for a CLR stored procedure streaming a result set via SendResultsStart and related methods, or Send(SqlDataRecord), it's vital because before those methods are called, SQL Server has no idea what the "shape"1 of the result set is, and it uses this metadata to understand it (and to be able to forward this metadata back to client applications also).




                1I often use shape when talking about result sets. It's the number of columns, their names and their data types.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 27 at 9:24

























                answered Mar 27 at 8:59









                Damien_The_UnbelieverDamien_The_Unbeliever

                202k18 gold badges264 silver badges354 bronze badges




                202k18 gold badges264 silver badges354 bronze badges



















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55369614%2fsqlmetadata-name-seems-irrelevant-when-passing-to-user-defined-table-type-via-sq%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript