无法在 SQL Server Management Studio 2008 中为 3 个视图生成脚本

发布于 2024-12-17 10:03:25 字数 796 浏览 1 评论 0原文

我有一个奇怪的问题

当我从 Sql Server 2008 创建对象脚本(用于删除和创建存储过程、视图、函数的脚本)时,它丢失了 3 个视图,不知道为什么?

我正在执行以下步骤来创建对象脚本

1) 打开 Sql Server 2008 Management Studio 2)连接到服务器 3) 右键单击​​选定的数据库,然后单击任务 ->生成脚本,然后从列表中选择数据库,单击下一步。 4)它提供了选项我正在更改三个选项,即包含如果不存在= true,脚本删除= true,脚本使用数据库= false并单击“下一步”按钮 4) 现在选择 SP、Views 和 Functions,然后单击 Next, 5) 对于所有即将出现的屏幕,单击全选 6) 最后单击完成按钮。

是否存在我未遵循的任何限制或特殊条件或约定,导致视图不包含在生成脚本中?

如果我遗漏了什么,请告诉我,我已经尝试了很多方法。

我还发现这个问题不仅存在于视图中,还存在于函数和存储过程中。

如果我们重命名它们,它就可以正常工作,例如,之前名为 dbo.SeperateElementsInt 的函数可以正常工作,但奇怪的是,生成脚本忽略了这个函数,后来我们将其重命名为 dbo.SeperateElementsInteger,它开始生成脚本。

我们无法更改视图名称,因为它在很多地方都使用。

出现问题的视图是 dbo.DivisionInfo 和 dbo.CustomerDivisonOfficeInfo 出现问题的存储过程是 dbo.procsync_get_zVariable

SSMS 2005 也存在该问题。

谢谢

I have a strange problem

When I create Object Script (script to drop and create Stored Procedures, Views, Functions) from Sql Server 2008 it misses 3 Views don't know why?

I am performing Following steps to create object script

1) Open Sql Server 2008 Management Studio
2) Connect to server
3) Right click on selected database then click on Tasks -> Generate Script, then select database from list, click Next.
4) It gives options I am changing three options i.e. Include If Not Exists = true, Script Drop = true, Script Use Database = false and clicing Next button
4) Now selecting SP, Views and Functions and clicking Next,
5) clicking Select All for All the coming screens
6) Finally clicking Finish button.

Is there any limitation or special condition or convention that I am not following and causing Views not to include in Generate Script?

Please let me know if I am missing something , I have tried many ways.

I also found that this problem not only exists with Views but it also exists with Functions and Stored Procedures.

If we rename them it works fine , for example a Function earlier named dbo.SeperateElementsInt was working fine, but strangely, Generate Script ignored this function, later we renamed it to dbo.SeperateElementsInteger and it started generating script.

We cannot change the View names as it is used at many places.

Views which are giving problem are dbo.DivisionInfo and dbo.CustomerDivisonOfficeInfo
Stored Procedure which is giving problem is dbo.procsync_get_zVariable

The problem exists with SSMS 2005 too.

Thanks

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

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

发布评论

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

评论(2

邮友 2024-12-24 10:03:25

我们在 INFORMATION_SCHEMA-profiler 问题上彼此不理解。我建议打开探查器,因为 SSMS 使用一些 where 子句对 INFORMATION_SCHEMA 执行 SELECT。我怀疑这个查询本身就切断了你的观点。一旦您有 SSMS 执行的查询来获取对象列表,您应该找到为什么它看不到某些视图。

以下是当您选择所有视图并开始编写脚本时 SSMS 执行的脚本。检查它们中是否有任何一个不返回 DivisionInfo 视图。 (我在数据库中创建了 DivisionInfo 视图来重现您的案例)。为了快速检查,请一一执行它们,并在每次查询后阅读我的评论。 请注意,您实际上应该使用 Profiler 捕获对您的环境的查询,因为它们可能会因您的环境而异。

在显示屏幕以选择视图、过程等之前,SSMS 会执行以下脚本来获取以下列表:观点:

exec sp_executesql N'SELECT
''Server[@Name='' + quotename(CAST(
        serverproperty(N''Servername'')
       AS sysname),'''''''') + '']'' + ''/Database[@Name='' + quotename(db_name(),'''''''') + '']'' + ''/View[@Name='' + quotename(v.name,'''''''') + '' and @Schema='' + quotename(SCHEMA_NAME(v.schema_id),'''''''') + '']'' AS [Urn],
v.name AS [Name],
SCHEMA_NAME(v.schema_id) AS [Schema]
FROM
sys.all_views AS v
WHERE
(v.type = @_msparam_0)and(CAST(
 case 
    when v.is_ms_shipped = 1 then 1
    when (
        select 
            major_id 
        from 
            sys.extended_properties 
        where 
            major_id = v.object_id and 
            minor_id = 0 and 
            class = 1 and 
            name = N''microsoft_database_tools_support'') 
        is not null then 1
    else 0
end          
             AS bit)=0)
ORDER BY
[Schema] ASC,[Name] ASC',N'@_msparam_0 nvarchar(4000)',@_msparam_0=N'V'

您的观点列出了吗?您可以添加条件 WHERE v.name = 'DivisionInfo' 进行过滤。如果没有列出 DivisionInfo,请检查此查询的哪一部分将其从结果集中删除。

一旦您选择要编写脚本的对象并开始编写脚本,SSMS 就会创建临时表,在其中存储对象并执行脚本来查找相关对象。

创建临时表并在其中插入 DivisionInfo 视图:

CREATE TABLE #tempdep (objid int NOT NULL, objname sysname NOT NULL, objschema sysname NULL, objdb sysname NOT NULL, objtypesmallint NOT NULL)

exec sp_executesql N'INSERT INTO #tempdep 

SELECT
v.object_id AS [ID],
v.name AS [Name],
SCHEMA_NAME(v.schema_id) AS [Schema],
db_name(),
2
FROM
sys.all_views AS v
WHERE
(v.type = @_msparam_0)and(v.name=@_msparam_1 and SCHEMA_NAME(v.schema_id)=@_msparam_2)',N'@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000)',@_msparam_0=N'V',@_msparam_1=N'DivisionInfo',@_msparam_2=N'dbo'

此查询是否在 #tempdep 中插入了任何内容?如果没有,请检查原因。 再一次,您必须使用 Profiler 从您的环境中获取查询,而不是使用我放在这里的查询,因为它们来自我的环境。

当您开始分析时,应该有许多像上面这样的插入。您需要找到与 DivisionInfo 相关的信息。您可以使用“查找”选项来查找它,因为您将在 Profiler 中看到许多查询,因为您有很多其他视图。要使探查器日志更小,请仅编写脚本视图。

正如您所看到的,想法是开始分析并开始编写脚本。脚本编写完成后,停止分析器并检查 SSMS 执行的脚本。您应该找出为什么它看不到 DivisionInfo。如果探查器日志中没有 DivisionInfo,但您可以在向导中检查它的脚本编写情况,然后为 DivisionInfo 和脚本适用的一个视图获取脚本,并查看它们之间的差异。仔细查看它们之间在 SMSS 用于检索它们的脚本方面的差异。

  • 由于某种原因,SSMS
  • 根据他通过查询提取的数据(从探查器捕获)丢弃了此视图

We didn't understand each other on INFORMATION_SCHEMA-profiler issue. I was suggesting to turn profiler on, because SSMS does a SELECT on INFORMATION_SCHEMA with some where clauses. I suspect that the query itself cuts off your views. Once You have a query that SSMS executes to get the list of objects You should find why it doesn't see some views.

Here are the scripts that SSMS executes when You select all views and start scripting. Check if any of them doesn't return DivisionInfo view. (I've created DivisionInfo view in my database to reproduce your case). For quick check execute them one by one and read my comments after each query. Please note that You should actually catch queries on your environment with Profiler, because they may differ on your environment.

Before showing screen to select views, procedures, ... SSMS executes following script to get the list of views:

exec sp_executesql N'SELECT
''Server[@Name='' + quotename(CAST(
        serverproperty(N''Servername'')
       AS sysname),'''''''') + '']'' + ''/Database[@Name='' + quotename(db_name(),'''''''') + '']'' + ''/View[@Name='' + quotename(v.name,'''''''') + '' and @Schema='' + quotename(SCHEMA_NAME(v.schema_id),'''''''') + '']'' AS [Urn],
v.name AS [Name],
SCHEMA_NAME(v.schema_id) AS [Schema]
FROM
sys.all_views AS v
WHERE
(v.type = @_msparam_0)and(CAST(
 case 
    when v.is_ms_shipped = 1 then 1
    when (
        select 
            major_id 
        from 
            sys.extended_properties 
        where 
            major_id = v.object_id and 
            minor_id = 0 and 
            class = 1 and 
            name = N''microsoft_database_tools_support'') 
        is not null then 1
    else 0
end          
             AS bit)=0)
ORDER BY
[Schema] ASC,[Name] ASC',N'@_msparam_0 nvarchar(4000)',@_msparam_0=N'V'

Is your view listed? You can add condition WHERE v.name = 'DivisionInfo' to filter it. If there is no DivisionInfo listed check what part of this query eliminates it from result set.

Once You select objects to script and start scripting, SSMS creates temp table, store objects in it and executes scripts to find related objects.

Create temp table and insert DivisionInfo view in it:

CREATE TABLE #tempdep (objid int NOT NULL, objname sysname NOT NULL, objschema sysname NULL, objdb sysname NOT NULL, objtype smallint NOT NULL)

exec sp_executesql N'INSERT INTO #tempdep 

SELECT
v.object_id AS [ID],
v.name AS [Name],
SCHEMA_NAME(v.schema_id) AS [Schema],
db_name(),
2
FROM
sys.all_views AS v
WHERE
(v.type = @_msparam_0)and(v.name=@_msparam_1 and SCHEMA_NAME(v.schema_id)=@_msparam_2)',N'@_msparam_0 nvarchar(4000),@_msparam_1 nvarchar(4000),@_msparam_2 nvarchar(4000)',@_msparam_0=N'V',@_msparam_1=N'DivisionInfo',@_msparam_2=N'dbo'

Did this query insert anything in #tempdep? If not, check why. Once again, You have to use Profiler to get queries from your environment instead of using queries I put here because they are from my environment.

When You start profiling, there should be many inserts like the one above. You need to find the one that relates to DivisionInfo. You can use Find option to find it because You will see many queries in Profiler because You have a lot of other views. To make profiler log smaller, script only views.

As You can see, idea is to start profiling and start scripting. Once scripting is finished, stop profiler and check scripts executed by SSMS. You should find why it doesn't see DivisionInfo. If there is no DivisionInfo in profiler log but You can check it for scripting in wizard, then take scripts for DivisionInfo and for one view that scripting works for and see the differences between them. Take a close look at differences between them in regards to scripts that SMSS uses to retrieve them.

  • for some reason SSMS discards this view
  • according to data he extracted with queries (catched from profiler)
哥,最终变帅啦 2024-12-24 10:03:25

我刚刚遇到了确切的问题。我们试图编写一个数据库(称为 Database_A)的架构,但许多视图无法编写脚本。

我们停用了另一个数据库(称为 Database_B),并且所有不会编写脚本的视图(在 Database_A 中)都指向该数据库 (Database_B) - 该数据库是通过链接服务器访问的,并且处于脱机状态。由于所有连接字符串现在都指向 Database_A 所在的新服务器,因此我将旧服务器上的 Database_A 以 read_only 方式联机,持续足够长的时间以编写视图脚本,并且它起作用了。再次使数据库脱机,我们就得到了我们需要的东西。

我编写的用于在视图中查找链接服务器引用的脚本是这样的:

    use Database_B
    go

    select so.name, sc.text 
    from sysobjects so, syscomments sc
    where so.id = sc.id
    and sc.text like '%Database_A%'

这对我有用,我希望它也对您有用。

保重,

汤姆

I just ran into the exact issue. We were trying to script out the schema of one database (Call it Database_A) and many views wouldn't script out.

We'd decommissioned another database (Call it Database_B) and all the views that wouldn't script (in Database_A) pointed out to that database (Database_B) - which was accessed through a linked server, and was offline. Since all the connection strings were now pointing to the new server that Database_A was now on, I brought Database_A on the old server online in read_only for just long enough to script out the views, and it worked. Took the database offline again, and we had what we needed.

The script I threw together to find the linked server reference in the views was this:

    use Database_B
    go

    select so.name, sc.text 
    from sysobjects so, syscomments sc
    where so.id = sc.id
    and sc.text like '%Database_A%'

That's what worked for me, I hope it works for you as well.

Take care,

Tom

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