生成多个和过滤的液滴 + 创建存储过程

发布于 2024-07-07 04:41:10 字数 543 浏览 8 评论 0原文

我有这个脚本:

select name,create_date,modify_date from sys.procedures order by modify_date desc

我可以看到最近修改了哪些程序。 我将添加一个“其中modify_date > =” 我想使用一些系统存储过程,它将生成我: drop + 为(比方说 5 个匹配的)存储过程创建脚本

我可以以某种方式执行此操作吗?

谢谢


,好的。 我有最终版本:

http://swooshcode。 blogspot.com/2008/10/generate-stored-procedures-scripts-for.html

你们帮了很多

忙,谢谢

I have this script:

select name,create_date,modify_date from sys.procedures order by modify_date desc

I can see what procedures were modified lately.
I will add a "where modify_date >= "
And I'd like to use some system stored procedure, that will generate me :
drop + create scripts for the (let's say 5 matching) stored procedures

Can i do this somehow?

thanks


ok. i have the final version:

http://swooshcode.blogspot.com/2008/10/generate-stored-procedures-scripts-for.html

you guys helped a lot

thanks

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

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

发布评论

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

评论(5

触ぅ动初心 2024-07-14 04:41:11

这不太漂亮,但很有效。 手动运行其输出或使用 sp_executesql 执行它。

SELECT OBJECT_DEFINITION(object_id), 'drop procedure [' + name + ']'
FROM   sys.procedures
WHERE modify_date >= @date

您还必须担心失去权利。

This ain't pretty, but it works. Run the output from it manually or execute it with sp_executesql.

SELECT OBJECT_DEFINITION(object_id), 'drop procedure [' + name + ']'
FROM   sys.procedures
WHERE modify_date >= @date

You will have to worry about lost rights as well.

关于从前 2024-07-14 04:41:11

不需要游标(根据模式等需要进行修改):

DECLARE @dt AS datetime
SET @dt = '10/1/2008'

DECLARE @sql AS varchar(max)

SELECT @sql = COALESCE(@sql, '')
    + '-- ' + o.name + CHAR(13) + CHAR(10)
    + 'DROP PROCEDURE ' + o.name + CHAR(13) + CHAR(10)
    + 'GO' + CHAR(13) + CHAR(10)
    + m.definition + CHAR(13) + CHAR(10)
    + 'GO' + CHAR(13) + CHAR(10)
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o
    ON m.object_id = o.object_id
INNER JOIN sys.procedures AS p
    ON m.object_id = p.object_id
WHERE p.modify_date >= @dt

PRINT @sql -- or EXEC (@sql)

No cursor necessary (modify as desired for schemas, etc):

DECLARE @dt AS datetime
SET @dt = '10/1/2008'

DECLARE @sql AS varchar(max)

SELECT @sql = COALESCE(@sql, '')
    + '-- ' + o.name + CHAR(13) + CHAR(10)
    + 'DROP PROCEDURE ' + o.name + CHAR(13) + CHAR(10)
    + 'GO' + CHAR(13) + CHAR(10)
    + m.definition + CHAR(13) + CHAR(10)
    + 'GO' + CHAR(13) + CHAR(10)
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o
    ON m.object_id = o.object_id
INNER JOIN sys.procedures AS p
    ON m.object_id = p.object_id
WHERE p.modify_date >= @dt

PRINT @sql -- or EXEC (@sql)
就像说晚安 2024-07-14 04:41:11

您可以使用游标来迭代每条记录:

DECLARE @spName NVARCHAR(128)
DECLARE myCursor CURSOR FOR SELECT name FROM sys.procedures ORDER BY modify_date DESC
OPEN myCursor
FETCH NEXT FROM myCursor INTO @spName
WHILE @@fetch_status = 0
BEGIN
    -- Process each stored procedure with a dynamic query
    PRINT @spName
FETCH NEXT FROM myCursor INTO @spName
END
CLOSE myCursor
DEALLOCATE myCursor

You could use a cursor to iterate through each record:

DECLARE @spName NVARCHAR(128)
DECLARE myCursor CURSOR FOR SELECT name FROM sys.procedures ORDER BY modify_date DESC
OPEN myCursor
FETCH NEXT FROM myCursor INTO @spName
WHILE @@fetch_status = 0
BEGIN
    -- Process each stored procedure with a dynamic query
    PRINT @spName
FETCH NEXT FROM myCursor INTO @spName
END
CLOSE myCursor
DEALLOCATE myCursor
方觉久 2024-07-14 04:41:11

与此同时,我做了一些挖掘,似乎就是

sp_helptext 'my_stored_procedure'

我需要的,(加上当我问问题时我已经知道的部分,jdecuyper 解释了更多)

meanwhile i did some digging, and seems like

sp_helptext 'my_stored_procedure'

is what i need, (plus the part that i already knew when i asked the question explained more by jdecuyper)

断念 2024-07-14 04:41:11

最好使用比 SQL 更合适的语言来完成此操作。 尽管有许多扩展,例如 T-SQL、PL/SQL 和 PL/pgSQL,但 SQL 并不是执行此任务的最佳选择。

这里是一个类似问题的链接,我的答案是使用 SQL-DMO 或 SMO,具体取决于您有 SQL 2000 还是 2005。

如何使用 c# 复制数据库

This is best done in a more suitable language than SQL. Despite its numerous extensions such as T-SQL, PL/SQL, and PL/pgSQL, SQL is not the best thing for this task.

Here is a link to a similar question, and my answer, which was to use SQL-DMO or SMO, depending on whether you have SQL 2000 or 2005.

How to copy a database with c#

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