SQL:插入之前的插入操作中的数据大小

发布于 2025-02-01 21:24:18 字数 319 浏览 5 评论 0原文

这听起来像是一个琐碎的问题,但我找不到任何直接的答案。

基本上,我有一个接受某些参数的SQL存储过程。参数将被处理以生成值,该值将在存储过程中以后在插入操作中使用。

现在,根据异常案例,传递的值可能会导致某些插入操作包含列的最大长度的值。我想捕捉这些方案,如果肯定会失败,则不要经历插入操作。

此外,如果确实发生了这种情况,我希望存储的过程以某种方式终止,以便调用模块知道它没有成功,类似于以多种语言进行异常处理中使用的类似。

更新:我知道我可以使用try/catch,但是它将在 Insights 中记录错误,并且我想防止此错误。

This sounds like a trivial problem and it probably is, but I couldn't find any straightforward answer anywhere.

Basically, I have a SQL stored procedure accepting some parameters. The parameters will be processed to generate values which will be used in an insert operation later on in the stored procedure.

Now, depending on exceptional cases, the passed values may result in some insert operations to contain values exceeding max length for a column. I would like to catch those scenarios and not go through the insert operation if it's certain to fail.

Additionally, if such a scenario does occur, I want the stored procedure to terminate in a manner so the calling module knows it didn't succeed, analogical to THROW as used in exception handling in many languages.

Update: I know I can use Try/Catch but it will log error in the insights and I want to prevent this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那片花海 2025-02-08 21:24:18

因此,您将需要在Sproc中使用尝试捕获块以进行错误处理。

BEGIN TRY

    PRINT 'Put Script in this block'

END TRY
BEGIN CATCH

    THROW

END CATCH

然后能够判断值是否会超过数据类型长度,以获取列的最大数据长度。您可以使用以下(如果有许可)来获得此功能,

SELECT column_name as 'Column Name', data_type as 'Data Type',
character_maximum_length as 'Max Length'
FROM information_schema.columns
WHERE table_name = 'tablename' 

最后您只需要获得变量的长度即可。您可以像下面一样做到这一点

SELECT LEN(@VariableName)

,然后您只需要将其放在一起

So you'll want to use a try catch block in your sproc for error handling.

BEGIN TRY

    PRINT 'Put Script in this block'

END TRY
BEGIN CATCH

    THROW

END CATCH

Then to be able to tell whether or not a value will exceed the data type length you will need to get the maximum data length of your column. You can get this using below (if you have permission)

SELECT column_name as 'Column Name', data_type as 'Data Type',
character_maximum_length as 'Max Length'
FROM information_schema.columns
WHERE table_name = 'tablename' 

Lastly you will just need to get the length of your variable. You can do that like below

SELECT LEN(@VariableName)

Then you just need to put it together

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文