How to pass large number of parameters in stored procedure from code to SQL Server

前端 未结 4 1546
情话喂你
情话喂你 2021-01-24 05:13

How to pass a large number of parameters (say 20+) to stored procedure from code?

Like we can group all the parameters in one class object and then pass it across, but h

4条回答
  •  [愿得一人]
    2021-01-24 05:37

    First, look at the design that requires so many parameters - is there a way you can reduce the need to send so much information to the server?

    Secondly, go back to the first point again because it still "smells" wrong :-)

    Then you can do things like:

    • Split the stored procedure. If you don't have to pass all the data in as a single related lump, then you could split one SP into several that do distinct parts of the overall action (e.g. update the users name and address details separately from adding their latest purchase, or whatever)

    • combine parameters. If you have five boolean values that indicate various options, then you could combine them into a single "flags" byte with bitwise operations. Or if you have two 32-bit ints that only hold values between 0..5,000, then they could be combined into a single 32-bit int. etc.

    • Serialise all the parameters into a "file format" and send the data as a single binary or text parameter, which you deserialise in your stored procedure.

    Of course, these approaches mean encoding and decoding a lot of parameters every time which may be worse than passing them all separately. Indeed, one could argue that the SQL call mechanisms are already doing the above for you.

提交回复
热议问题