sql-view

Why can't SQL Server alter a view in a stored procedure?

风格不统一 提交于 2019-11-29 07:37:40
I'm using MS SQL Server, and I'd like to alter a view from within a stored procedure, by executing something like "alter view VIEWNAME as ([some sql])". A few pages thrown up by google assert that this is not supported directly (and neither are related alter-table statements), but there are also examples of how to work around it using constructions like this: declare @sql varchar(max) select @sql = 'alter view VIEWNAME as ([some sql])' exec(@sql) Writing code as literal strings smells a bit, even for SQL. My questions: Why is this not supported? What's the difference between running this from

How do read-only database views fit into the repository pattern?

北城以北 提交于 2019-11-28 16:50:51
问题 Example: Your database has a SQL view named "CustomerOrdersOnHold". This view returns a filtered mix of specific customer and order data fields. You need to fetch data from this view in your application. How does access to such a view fit into the repository pattern? Would you create a "CustomerOrdersOnHoldRepository"? Is a read-only view such as this considered an aggregate root? 回答1: I would prefer separating the read repository, preferably even change its name to Finder or Reader, the

Table-Valued Function(TVF) vs. View

◇◆丶佛笑我妖孽 提交于 2019-11-28 15:20:36
What's the difference between table-valued functions and views? Is there something you can do with 1 that's hard or impossible to do with the other? Or does the difference lie in efficiency? Martin Smith A parameterless inline TVF and a non materialized View are very similar. A few functional differences that spring to mind are below. Views Accepts Parameters - No Expanded out by Optimiser - Yes Can be Materialized in advance - Yes (through indexed views) Is Updatable - Yes Can contain Multiple Statements - No Can have triggers - Yes Can use side-effecting operator - Yes Inline TVFs Accepts

Creating a View using stored procedure

北城以北 提交于 2019-11-28 09:40:41
问题 This questions have asked few times before, unfortunately I did not get an answer to my questions. Well I have two SQL ( SQL SERVER 2008 ) tables, Employee and Employee expens, where Employee Id is the Primary key and the foreign key respectively. Employee table columns, 1. Employee Id (P Key) 2. Manager 3. Location 4. Join Date 5. Name Employee Expense table columns, 1. Expense Id (P Key) 2. Employee Id (F key) 3. Expense Type 4. Expense Amount 5. Expense Date. Question is, I want to create

How to make a view column NOT NULL

本小妞迷上赌 提交于 2019-11-28 06:40:01
I'm trying to create a view where I want a column to be only true or false. However, it seems that no matter what I do, SQL Server (2008) believes my bit column can somehow be null. I have a table called "Product" with the column "Status" which is INT, NULL . In a view, I want to return a row for each row in Product, with a BIT column set to true if the Product.Status column is equal to 3, otherwise the bit field should be false. Example SQL SELECT CAST( CASE ISNULL(Status, 0) WHEN 3 THEN 1 ELSE 0 END AS bit) AS HasStatus FROM dbo.Product If I save this query as a view and look at the columns

Create a sql view based converting ranges into rows

﹥>﹥吖頭↗ 提交于 2019-11-28 05:49:10
问题 I have a table structured like so ColA|ColB|LowRange|HighRange ---------------------------- 1 A 1 5 I would like to create a view that will make the data available in the following format ColA|ColB|RangeNumber ---------------------- 1 A 1 1 A 2 1 A 3 1 A 4 1 A 5 I'm not familiar enough with views so I need some direction. Thanks 回答1: You can accomplish this using a recursive CTE CREATE TABLE ranges ( ColA int, ColB char, LowRange int, HighRange int, ); INSERT INTO ranges VALUES (1, 'A', 1, 5)

Cannot update view?

核能气质少年 提交于 2019-11-28 00:12:05
My site was developed using Drupal 6 running on a Postgresql 8.3 server on Ubuntu 11.10. Also webmin version 1.590. Now I want to update records in a table, but when I run: UPDATE uac_institution_view SET status = '2' WHERE nid = '9950' it gives me an error like: Failed to execute SQL : SQL UPDATE uac_institution_view SET status = '2' WHERE nid = '9950' failed : ERROR: cannot update a view HINT: You need an unconditional ON UPDATE DO INSTEAD rule. The problem is that only SELECT queries work. UPDATE , INSERT and DELETE commands are not working; they fail with the above error. Is this a

How do MySQL views work?

旧巷老猫 提交于 2019-11-27 14:16:42
When I create a view I am basically making a new table that will automatically be transacted upon when data in one of the tables it joins changes; is that correct? Also why can't I use subqueries in my view? A view works like a table , but it is not a table. It never exists; it is only a prepared SQL statement that is run when you reference the view name. IE: CREATE VIEW foo AS SELECT * FROM bar SELECT * FROM foo ...is equivalent to running: SELECT x.* FROM (SELECT * FROM bar) x A MySQLDump will never contain rows to be inserted into a view... Also why can't I use subqueries in my view????

SQL Updatable View with joined tables

霸气de小男生 提交于 2019-11-27 02:47:10
问题 I have a view that looks similar to this, SELECT dbo.Staff.StaffId, dbo.Staff.StaffName, dbo.StaffPreferences.filter_type FROM dbo.Staff LEFT OUTER JOIN dbo.StaffPreferences ON dbo.Staff.StaffId = dbo.StaffPreferences.StaffId I'm trying to update StaffPreferences.filter_type using, UPDATE vw_Staff SET filter_type=1 WHERE StaffId=25 I have read this in an MSDN article, Any modifications, including UPDATE, INSERT, and DELETE statements, must reference columns from only one base table. Does this

How to make a view column NOT NULL

不打扰是莪最后的温柔 提交于 2019-11-27 01:26:12
问题 I'm trying to create a view where I want a column to be only true or false. However, it seems that no matter what I do, SQL Server (2008) believes my bit column can somehow be null. I have a table called "Product" with the column "Status" which is INT, NULL . In a view, I want to return a row for each row in Product, with a BIT column set to true if the Product.Status column is equal to 3, otherwise the bit field should be false. Example SQL SELECT CAST( CASE ISNULL(Status, 0) WHEN 3 THEN 1