简体   繁体   English

SQL Server中搜索功能的快捷方式

[英]Shortcut for search functionality in SQL Server

I want to create shortcut for below SQL code 我想为下面的SQL代码创建快捷方式

DECLARE @Proc_Name VARCHAR(255)
SET @Proc_Name ='norway'
SELECT * FROM sys.tables WHERE name LIKE '%' + @Proc_Name + '%'

Right now I am using below shortcut for same 现在我正在使用以下快捷方式 在此处输入图片说明

Then I type {LIKE '%test%'} and press Ctrl+8. 然后,我输入{LIKE'%test%'},然后按Ctrl + 8。

But I want to make it more simplified format(Like I can type {test} and press Ctrl+8) so that I can use it quickly. 但是我想使其更加简化(例如,我可以键入{test}并按Ctrl + 8),以便可以快速使用它。

I was trying same in different way like creating proc > then execute > and drop it on shortcut key press. 我尝试以不同的方式进行尝试,例如创建proc>然后执行>并将其放在快捷键上。 For more info please visit below link: 有关更多信息,请访问以下链接:

Single line GO(Batch) statement giving error in SQL Server? 单行GO(Batch)语句在SQL Server中给出错误?

Thanks, 谢谢,

Vishal 维沙尔

I have the following stored proc that I added to the Master db on my SQL Server 我将以下存储的过程添加到SQL Server的主数据库中

USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE [dbo].[SP_FindAllReferences]
@targetText nvarchar(128)
AS
BEGIN
    SET NOCOUNT ON;

    declare @origdb nvarchar(128)
    select @origdb = db_name()

    declare @sql nvarchar(1000)

    set @sql = 'USE [' + @origdb +'];' 
    set @sql += 'select object_name(m.object_id), m.* '
    set @sql += 'from sys.sql_modules m  where m.definition like N' + CHAR(39) + '%' + @targetText + '%' + CHAR(39)

    exec (@sql)

    SET NOCOUNT OFF;
END

Then I have added dbo.SP_FindAllReferences to one of my SSMS keyboard shortcuts so that I can search for text in the context of any DB on my server. 然后,将dbo.SP_FindAllReferences添加到了我的SSMS键盘快捷方式之一,以便可以在服务器上任何数据库的上下文中搜索文本。

For example, if I type user in a SSMS query window, highlight it and press the shortcut combo, then I get a list of every stored proc in the current database that contains user 例如,如果我在SSMS查询窗口中键入user ,突出显示它并按下快捷键组合,那么我会得到包含用户的当前数据库中每个已存储proc的列表。

If you replace my select with 如果您将我的选择替换为

set @sql = 'USE [' + @origdb +'];' 
set @sql += 'Select * FROM sys.tables WHERE name LIKE N' + CHAR(39) + '%' + @targetText + '%' + CHAR(39)

then you be able to achieve your goal. 这样您就可以实现自己的目标。

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

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