如何使用 sqlcmd.exe 调用带参数的存储过程? [英] How do I call a stored procedure with arguments using sqlcmd.exe?

查看:53
本文介绍了如何使用 sqlcmd.exe 调用带参数的存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要调用一个存储过程并从 Powershell 传入参数.我认为最好的选择是使用 sqlcmd.exe,但我不确定如何使用此工具将参数传递给存储过程.

I need to call a stored procedure and pass arguments in from Powershell. I think the best option is to use sqlcmd.exe but I'm not sure how to pass arguments to the stored proc using this tool.

推荐答案

sqlcmd.exe 通过 /v 参数支持变量替换和参数,请参阅 将 sqlcmd 与脚本变量一起使用.例如:

sqlcmd.exe supports variable substitution and parameters via the /v argument, see Using sqlcmd with Scripting Variables. For example:

sqlcmd -E -d <mydb> -Q "exec usp_myproc @variable=$(myparam)" /v myparam=1

将调用将值 1 传递给脚本以替换变量 $(myparam) 的过程.请注意,在将批处理(请求)发送到 SQL Server 之前,sqlcmd 变量替换是在 sqlcmd 中发生的 $(variable) 的字符串替换.

will invoke the procedure passing the value 1 to the script to be substituted for the variable $(myparam). Note that sqlcmd variable substitution is a string replacement of $(variable) which occurs in sqlcmd, befor the batch (request) is sent to the SQL Server.

这篇关于如何使用 sqlcmd.exe 调用带参数的存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆