简体   繁体   English

存储过程以创建视图

[英]stored procedure to create a view

Ok, So i know this questions has been asked. 好的,我知道这个问题已经被问到了。 and everything i read has been "well.. you can do it using dynamic DSL, but dont do it" my question is why. 我读到的一切都“很好......你可以用动态DSL来做,但不要这样做”我的问题是为什么。 I'm still new at this so I'm learning so bear with me but here is what i'm doing. 我还是新手,所以我正在学习如此忍受我,但这就是我正在做的事情。 I want to use a stored procedure to create a dynamic view (but not a temporary table) the view has two dates that it uses to establish a beginning and ending date. 我想使用存储过程来创建动态视图(但不是临时表)视图有两个日期用于建立开始和结束日期。 it looks something like this: 它看起来像这样:

create or replace view MyView as
SELECT
   A.COLUMN_A
   FUNCTION1(to_date('2/10/2011','MM/DD/YYYY') TOTAL1,
   FUNCTION2(to_date('2/15/2011','MM/DD/YYYY') TOTAL2
FROM TABLE_A A;

This view is then used to generate the data needed for a report in crystal. 然后,此视图用于生成Crystal中报表所需的数据。 The problem is that we are about to start using these same sql statements in another language. 问题是我们即将开始在另一种语言中使用这些相同的sql语句。 (we are currently using delphi but to about working on another language (but i dont know what the other language is)) the reason I want to create the view in a stored procedure is, because a) the view is dynamic, and based off the date range selected by the user and b) instead of having to put in some rather large views in multiple languages (that have to be created on the fly due to the changing date range selection) onlt the single line for the function and the parameters would need to be passed. (我们目前正在使用delphi,但是关于使用另一种语言(但我不知道另一种语言是什么))我想在存储过程中创建视图的原因是,因为a)视图是动态的,并且基于用户选择的日期范围和b)而不是必须在多行语言中放入一些相当大的视图(由于更改日期范围选择而必须在运行中创建),而不是在函数和参数的单行上需要通过。 Alot of what i read has said that using Dynamic SQL to create a view is bad, but knowing that its already a dynamic view created specifically for the user on the fly, does anyone see an issue with that? 我读到的很多内容都说使用动态SQL创建视图很糟糕,但是知道它已经是一个专门为用户动态创建的动态视图,有没有人看到这个问题? I'm asking because I dont want to get myself into something down the road that I wouldnt be able to get myself out of with out wanting to yank all my hair out. 我问,因为我不想让自己陷入困境,因为我不想把自己弄出来,想要把我所有的头发都拉出去。

I do Delphi front-ends and SQL Server backends. 我做Delphi前端和SQL Server后端。 Why use a view ? 为什么要使用视图? I always just create a SP with a simple SELECT which does the job very nicely. 我总是用一个简单的SELECT创建一个SP,它可以很好地完成工作。 If the Crystal Report is used frequently which is usually the case) I simply create a permanent table which is cleared and repopulated each time the SP is run. 如果频繁使用Crystal Report(通常是这种情况),我只需创建一个永久表,每次运行SP时都会清除并重新填充。 Short, sweet, and simple. 简短,甜美,简单。

Creating a view (or any database object) dynamically is like manipulating your code at runtime. 动态创建视图(或任何数据库对象)就像在运行时操作代码一样。 Powerfull in good and bad ways. 在好的和坏的方面强大。

The riscs and problems you might run into: 您可能遇到的危险和问题:

  • performance: DDL statements will invalidate some of your cache, which is bad for performance 性能:DDL语句会使一些缓存失效,这对性能有害
  • fatal bugs: If other stuff depends on the view (stored procedures, other views ...) they will break if your view breaks for one reason or another. 致命的错误:如果其他东西依赖于视图(存储过程,其他视图......),如果您的视图由于某种原因而中断,它们将会中断。
  • scalability: DDL will need a exclusive lock on that object. 可伸缩性:DDL需要对该对象进行独占锁定。 It might take long to obtain that and blocks everybody else while holding it. 可能需要很长时间才能获得并阻止其他人。
  • scalability: What happens if two users need to change the view in different ways at the same time? 可伸缩性:如果两个用户需要同时以不同方式更改视图,会发生什么?
  • maintainability: It gets hard to understand what is going on when objects change on the fly 可维护性:当对象在运行中发生变化时,很难理解发生了什么
  • security: Somebody has to have the right to change these objects, which might get misused for doing evil stuff. 安全性:有人必须有权更改这些对象,这些对象可能会因为做坏事而被滥用。

In 99.9% one doesn't need dynamic creation of objects and if you aren't in the 0.1 percent you shouldn't use them. 在99.9%中,不需要动态创建对象,如果不是0.1%,则不应使用它们。

Given your descripton of the task at hand: Why don't you just use the sql statement with the variable parts as bind variables and use that? 鉴于您对手头任务的描述:为什么不直接使用带有变量部分的sql语句作为绑定变量并使用它? I don't see the need for a view here. 我不认为这里需要一个视图。

Generating a view is only a good idea if it's really code-generation and generally fairly permanent. 生成视图只是一个好主意,如果它真的是代码生成并且通常是相当永久的。 Because it's a schema change, it should be viewed as an advanced technique, and I always try to use database techniques in order of complexity. 因为它是模式更改,所以应该将其视为一种高级技术,并且我总是尝试按照复杂性的顺序使用数据库技术。

ie For certain table/view designs, if it can be done with a view, try that. 即对于某些表/视图设计,如果可以使用视图完成,请尝试。 If an indexed view is really needed for performance, try that. 如果性能确实需要索引视图,请尝试这样做。 Alternatively, maybe a computed column or a persisted computed column. 或者,可以是计算列或持久计算列。 Perhaps an inline table-valued function, or a multi-statement table-valued function, or potentially a stored procedure. 也许是内联表值函数,或多语句表值函数,或者可能是存储过程。 So I try to escalate only when necessary. 所以我只在必要时尝试升级。

In your case, that "view" can also be an inline table-valued function in SQL Server (and DB2 at least) and such a creature is just like a parametrized view. 在您的情况下,“视图”也可以是SQL Server中的内联表值函数(至少是DB2),这样的生物就像参数化视图。 You can also use a parametrized stored procedure directly from most reporting tools (and of course from most language/db libraries). 您还可以直接从大多数报告工具(当然还有大多数语言/数据库库)使用参数化存储过程。

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

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