存储过程的多个参数
我需要从包含 96 个字段的记录中选择 45 个字段(不要问我,但我无法标准化它们,如果可以的话我会这样做)。所以,我有这个页面,一旦加载到用户面前,就需要所有这 45 个页面。
基本上,我在想,我会创建一个新的存储过程,它将检索所有字段名并将它们放入一个字段和所有值并将它们放入另一个字段,基本上最终会得到两个参数。然后我最终会用 C# 处理它们。
现在我的问题是,1,这样做正确吗?第二,如果是的话,我不知道如何选择字段并将其放在一个参数上。
select @sql = ' select 'field1' + 'field2' + 'field3'.....
我很困惑从哪里开始?
I need to select 45 fields from a record which has 96 of them (dont ask me, but i can't normalize them, i would if i could). So, I have this page who would need all those 45 on them once it's loaded in front of the user.
Basically, I was thinking, I would make a new stored proc that would retrieve all the fieldnames and put them into one field and all the values and put them into another field and basically would end up with two parameters. I would then end up processing them in C#.
Now my question is, 1, is it the right way to do it? 2nd, if it is I can't figure out how to select the fields and put it on one parameter.
select @sql = ' select 'field1' + 'field2' + 'field3'.....
im confused on where to start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一方面,你让这种方式变得比它需要的更加复杂。我永远不会知道一张表上到底如何有 96 列,但要选择您需要的 45 列,您只需在 select 语句中键入 45 列即可。
下面是 SQL 的示例。当然,我不会输入 45 列,但您明白了:
我想解决的另一个问题是执行 SQL 语句的方式。永远不要将字符串变量连接到一个 SQL 字符串中。确保您至少使用参数化查询。但我建议有时也研究一下 Entity Framework 或 LINQ to SQL。
那^^^就等于很糟糕。想象一下,如果用户决定偷偷摸摸地将他的名字设为
Harry' AND Admin='true
,会发生什么。您可能会想,“哦,好吧,我只需对所有变量执行firstName = firstName.Replace("'","''");
即可。如果您这样做,我会亲自来打孔像这样参数化您的查询:编辑 ^^^ 等于更好。
此外,如果您有机会重新处理您拥有的庞大表,请尝试将字段子集重构到它们中。自己的实体(表)并通过引用 ID 链接它们,例如,假设我有一个名为 [tlbUsers] 的表,它包含有关特定用户的信息,如下所示:
考虑重构该表,以便相关值拥有自己的表。可以从该用户表中获取所有地址信息并将其放入名为 tlbAddresses 的表中,这不仅可以使提取数据时更容易处理,而且还可以节省数据库空间。哈利和莎莉住在同一个家里,他们可以引用相同的地址记录。
Well for one thing you are making this way more complex than it needs to be. How in the world you have 96 columns on one table I will never know, but to select the 45 you need you're just going to have to type out 45 columns in the select statement.
Here is a sample of what the SQL would look like. Naturally I'm not going to type 45 columns, but you get the idea:
The other issue I would like to address is the way you are executing your SQL statement. NEVER EVER EVER EVER EVER EVER EVER concatenate string variables into one SQL string. Make sure you are using parameterized queries at the very least. But I would recommend looking into Entity Framework or LINQ to SQL sometime as well.
That ^^^ equals very bad. Think about what would happen if a user decided to be sneaky and make his first name
Harry' AND Admin='true
. You might think, "Oh, well I'll just dofirstName = firstName.Replace("'","''");
on all my variables. If you do that I will personally come punch you. Parameterize your queries like this:That ^^^ equals much better.
EDIT Also if you ever get a chance to re-work that monster of a table you have, try refactoring subsets of fields into their own entity (table) and linking them via a reference ID. For example, say I have a table called [tlbUsers] and it contains info about a specific user. Like this:
Consider refactoring that so that related values have their own table. You could take all the address info from this users table and put it in a table called tlbAddresses. Not only would that make it easier to deal with when pulling in the data, but it could potentially save you space in the database. For instance, if Harry and Sally both live in the same home, they could reference the same address record.
我在理解你的问题时遇到了一些困难,但是如果你想将可变数量的参数传递给存储过程,我可以想到两种方法,分别需要 SQL Server 2005 和 SQL Server 2008。
第一个利用 XML。让您的过程采用 varchar(max) 参数,然后您可以轻松地将其拆分出来。例如,如果您用逗号分隔您想要的内容,您可以:
此外,您可以利用表值变量并选择表中的输入并将其传递给存储过程。请参阅http://www.sqlteam.com/article/sql-以 server-2008-table-valued-parameters 为例。
I am having a little trouble understanding your question, however if you want to pass a variable number of parameters to a stored procedure there are two ways I can think of that you can do it, which require SQL Server 2005 and SQL Server 2008 respectively.
The first leverages XML. Have your procedure take a varchar(max) argument and then you can easily split it out. For example, if you comma separate what you want, you could:
Also, you could leverage table valued variables and select your inputs into a table and pass that to the stored procedure. See http://www.sqlteam.com/article/sql-server-2008-table-valued-parameters for an example.
您可以在一个字段中以 xml 形式返回数据。
测试表
测试数据
查询
结果
XMLData 字段中的 XML
You can return the data as xml in one field.
Test table
Test data
Query
Result
XML in XMLData field