如何在Management Studio中编写参数化查询? [英] How to write a parametrized query in management studio?

查看:105
本文介绍了如何在Management Studio中编写参数化查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通常从客户端应用程序执行

From a client application I tyipically do:

select * from table where Name = :Parameter

然后在执行查询之前我会做

and then before executing the query I do

:Parameter = 'John'

这些参数不是Search& Replace,而是传递给服务器的实际参数.由于我需要详细测试其中一些查询,因此如何在Management Studio中编写查询?

These parameters are not a Search&Replace but real parameters passed to the server. Since I need to test some of those queries in detail, how can I write the query in management studio?

我想用参数写查询并给参数赋值.该怎么办?

I want to write the query with parameters and give a value to the parameter. How can this be done?

更新:

在这里,为了消除混乱,我添加了一些信息以更好地表达自己的见解.

To remove confusion here I add info to better express myseld.

当我执行普通查询时,我会在sql server profiler中看到

when I execute a normal query I see in sql server profiler

select * from table where Name = 'John'

当我执行参数化查询时,我看到以下内容:

while when I execute a parametrized query I see this:

exec sp_executesql N'select * from table 
where Name = @P1',N'@P1 varchar(8000)','John'

这就是为什么我说这不是搜索并替换的原因.

This is why I say it is not a search and replace.

推荐答案

类似的东西

DECLARE @Parameter VARCHAR(20)
SET @Parameter = 'John'

SELECT *
FROM Table
WHERE Name = @Parameter

这篇关于如何在Management Studio中编写参数化查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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