简体   繁体   English

sql server存储过程使用varchar max作为参数

[英]sql server stored procedure use varchar max as parameter

I have a relatively simple website which uses php + mssql. 我有一个相对简单的网站,使用php + mssql。 I have various stored procedures that do simple insert and delete work. 我有各种存储过程,可以进行简单的插入和删除工作。 The problem I am facing is that many of the parameters that I pass into the stored procedure have the same length as the column I will insert the data. 我面临的问题是,我传入存储过程的许多参数与插入数据的列的长度相同。

For instance I have a stored procedure to register a user. 例如,我有一个存储过程来注册用户。 The stored procedure has a paramater @USERNAME with varchar(12) as the type. 存储过程有一个参数@USERNAME其中varchar(12)作为类型。 the columnn USERNAME in the USER table has the same type with that length. USER表中的columnn USERNAME与该长度具有相同的类型。 In principle this is not really a problem. 原则上这不是一个真正的问题。 But I would rather be able to freely change the USERNAME column length without having to also change the length of the stored procedure parameter. 但我宁愿能够自由更改USERNAME列长度​​,而不必更改存储过程参数的长度。

I can't use table valued parameters since the mssql php drivers doesn't support them. 我不能使用表值参数,因为mssql php驱动程序不支持它们。

The only solution I can imagine is to just use varchar(max) for the stored procedure parameters, but is this considered bad practice? 我能想象的唯一解决方案是只使用varchar(max)作为存储过程参数,但这被认为是不好的做法吗?

Short answer: Yes. 简短回答:是的。

What would it mean if someone passed in a username that was 2,147,483,647 characters long!? 如果有人传入长度为2,147,483,647个字符的用户名,这意味着什么? By using varchar(max) you're implying that it would be ok. 通过使用varchar(max),你暗示它会没问题。

I'm in agreement with the comment by @DanGuzman. 我同意@DanGuzman的评论。 Table columns should be sized properly in the first place then paramaters and variables should be sized accordingly. 表格列的大小应该在第一位正确,然后参数和变量应相应地调整大小。 If you have to change the size of the table column then you have to propogate that change elsewhere - that's the best practice approach. 如果你必须改变表格列的大小,那么你必须在其他地方传播这种变化 - 这是最佳实践方法。

Sizing table columns properly isn't always easy though - how long should a username be? 正确调整表格列的大小并不总是很容易 - 用户名应该多长时间? 12 seems short to me, 2,147,483,647 definitely too long - somewhere in between? 12对我来说似乎很短,2,147,483,647肯定太长了 - 介于两者之间? ;) @JohnCappelletti has a valid point too - storage and processing is cheap compared to the cost of change, so err on the large side when sizing. ;)@JohnCappelletti也有一个有效的观点 - 与变更成本相比,存储和处理成本低廉,因此在调整大小时错误。

If varchar(max) is bad, and you don't want to follow best practice, is there a middle ground? 如果varchar(max)很糟糕,并且你不想遵循最佳实践,那么它是否存在中间立场? Maybe use a factor of 10? 也许用10倍? Something sufficiently above the actual size but not excessivly so. 足够高于实际尺寸但不过分的东西。

The other thing to watch out for, regardless of how you size your parameters, is silent truncation or run-time truncation errors - check lengths explicitly. 无论您如何调整参数大小,另外需要注意的是静默截断或运行时截断错误 - 明确检查长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM