Hai, I'm an novice in Sql Server. I wanna know why we are going for VIEWS instead of SELECT statement in Sql Server 2005. However, both are going to perform the same task (i.e, Retreiving data) from the database.
People are telling me it is bad to putselect * from <atable>in a view. I better should list all fields of the table inside thedefinition of the view.I dont know exactly why but some say:A select * from makes sql server does a table scan.Is that true, even if i put a where on the select on the view? And what if idont list all fields in the select on the view?Thanks for the answer.Peter
Iam having 2 tables say Emp table and Department Table. Iam having a view which joins 2 tables. example select *,Department.DeptStatus, Department.DepartmentName from Employee INNER JOIN Department ON Department.deptno=Employee.DeptNO. When I add a new column to the Employee table say EmployeeStatus then my view get corrupted.what i mean is that the last newly added column from emloyee table takes the Name as DeptStatus and shows values of the EmployeeStatus Column. We have been creating new Columns in SQL 2000 we never faced this issue but iam seeing this issue only in SQL Server 2005.
How do , I grant select privileges on all the Views of the database to a user in SQl 2005. All the tables and views in the database are under the dbo schema.
I want to be able to select data from a database without using grid view etc. I can set up a connection and bind it to a grid view but I would like to do some data processing without showing it. Something like
select id from table where name = "x" store id in idvar store the row count to a variable rowcount if rowcount > 0 select name from table2 where id = idvar endif
etc
I can see how to set the sqldatasource select command but not how to run it and get results out of it. Please help
I've had a couple problems with SMO and I wonder if anyone else has. Specifically, when I run through stored procedures (just like your example) it is painfully slow. Maybe 5 a second. Another problem is filtering on nonsystem objects with either "SVR.SetDefaultInitFields(typeof(Microsoft.SqlServer.Management.Smo.View), "IsSystemObject"); or View.isSystemObject. Both cases crash my app. Any ideas? Below are two ways that both crash or hang. SVR.SetDefaultInitFields(typeof(Microsoft.SqlServer.Management.Smo.View), "IsSystemObject"); foreach (Microsoft.SqlServer.Management.Smo.View view in viewCollection) {} OR
foreach (Microsoft.SqlServer.Management.Smo.View view in viewCollection) { if (!view.IsSystemObject) {} }
VIEW behaviour depending on what operation is run on it.
VIEW syntax is CREATE VIEW xx AS SELECT ... It would be very convinient to know wether a select, update, insert or delete is the operation executed on it so the view could take action accordingly, ie CREATE VIEW xxx AS SELECT .. CASE UPDATE THEN do_this CASE SELECT THEN do_that etc. Does anyone know if this is possible?
If I have a view such as: SELECT T.* FROM T When I add a column to table T the view is not updated to reflect that change. Furthermore, if there are other columns after the * in the view (for example SELECT T.*, GETDATE() as "My Date" FROM T) the last columns will contain incorrect data.
Is there a work around for this? An "auto-recompile when tables are modified" kind of option?
Thanks Nick
PS: This is the script I used for testing:
create table tt ( test1 int primary key, test2 int) go insert into tt (test1, test2) values (1,2) go create view vw_tt as select *, getdate() as "My Date" from tt go select * from vw_tt go create view vw_tt2 as select * from tt go alter table tt add test3 int go select * from vw_tt select * from vw_tt2 select * from tt drop table tt drop view vw_tt drop view vw_tt2
I am building a dashboard features that allows user to select reports from a dropdownlist. It is pulling from a table called Reports (cols: ReportID, Description, sqlView) In this Report table the report is associated to a view that queries the report. And the user's selections are stored in table called UserReport (cols: userID, ReportID, createDt) . I need to get a Dataset to contain datables of all reports selected. (for example a user select 3 reports, the dataset should contain 3 datables that represent the report). I want to accomplish this by create a store procedure that queries the Reports table and then dynamically executes the views that related to the user selected reports. Can anyone give me an example on how to create the storeprocedure? Thanks, CG
I'm trying to grant a user group select access to all the views in a database. I already made a query which creates a result set whit the SQL Syntax I (displayed below) need but it seems to be impossible to get this result set executed after creation.
use [AdventureWorksDW2008R2] SELECT 'GRANT SELECT ON [' + SCHEMA_NAME(Schema_id) + '].[' + name + '] TO [DOMAINGROUP]' FROM sys.views;
Hi all,We have an application through which we are bulk inserting rows into aview. The definition of the view is such that it selects columns froma table on a remote server. I have added the servers usingsp_addlinkedserver on both database servers.When I call the Commit API of oledb I get the following error:Error state: 1, Severity: 19, Server: TST-PROC22, Line#: 1, msg:SqlDumpExceptionHandler: Process 66 generated fatal exception c0000005EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.I would like to know if we can bulk insert rows into a view thataccesses a table on the remote server using the "bulk insert" or bcpcommand. I tried a small test through SQL Query Analyser to use "bulkinsert" on a such a view.The test that I performed was the following:On database server 1 :create table iqbal (var1 int, var2 int)On database server 2 (remote server):create view iqbal as select var1,var2 from[DBServer1].[SomeDB].[dbo].[iqbal]set xact_abort onbulk insert iqbal from '\MachineIqbaliqbaldata.txt'The bulk insert operation failed with the following error message:[Microsoft][ODBC SQL Server Driver][DBNETLIB]ConnectionCheckForData(CheckforData()).Server: Msg 11, Level 16, State 1, Line 0General network error. Check your network documentation.Connection BrokenThe file iqbaldata.txt contents were :112233If the table that the view references is on the same server then weare able to bulk insert successfully.Is there a way by which I should be able to bulk insert rows into aview that selects from a table on a remote server. If not then couldanyone suggest a workaround. I would actually like to know someworkaround to get the code working using OLEDB. Due to unavoidablereasons I cannot output the records to the file and then use bcp tobulk insert the records in the remote table. I need to have some wayof doing it using OLEDB.Thanks in advanceIqbal
Fellow database developers,I would like to draw on your experience with views. I have a databasethat includes many views. Sometimes, views contains other views, andthose views in turn may contain views. In fact, I have some views inmy database that are a product of nested views of up to 6 levels deep!The reason we did this was.1. Object-oriented in nature. Makes it easy to work with them.2. Changing an underlying view (adding new fields, removing etc),automatically the higher up views inherit this new information. Thismake maintenance very easy.3. These nested views are only ever used for the reporting side of ourapplication, not for the day-to-day database use by the application.We use Crystal Reports and Crystal is smart enough (can't believe Ijust said that about Crystal) to only pull back the fields that arebeing accessed by the report. In other words, Crystal will issue aSelect field1, field2, field3 from ReportingView Where .... eventhough "ReportingView" contains a long list of fields.Problems I can see.1. Parent views generally use "Select * From childview". This meansthat we have to execute a "sp_refreshview" command against all viewswhenever child views are altered.2. Parent views return a lot of information that isn't necessarilyused.3. Makes it harder to track down exactly where the information iscoming from. You have to drill right through to the child view to seethe raw table joins etc.Does anyone have any comments on this database design? I would love tohear your opinions and tales from the trenches.Best regards,Rod.
Newbie here. I've only been using SQL for about a year now and have some minor questions about sql objects that reference other objects.
We have some views which reference other views in the joins. I will call one the primary view and the one being referenced in the joins as the secondary view.
Recently we made changes to the secondary view.
After which the primary views which referenced it would not work because of this change and had to be 'refreshed' by using drop/create scripts which essentially just dropped it and recreated the exact same view. I do not recall the exact error message that was returned other than it seemed to suggest that it could no longer see the secondary view since it had been changed. Nothing in the primary view was changed in any way, just the secondary.
Some here where I work have suggested off hand that this was a recompile of the primary view because the contents of the secondary changed.
My questions are:
1. Exactly why did this happen and is there a proper name for it when it does?
2. The same problem does not seem to occur when we have stored procedures referencing views in the joins which had just been changed. Why is that?
Thanks for any help on the matter. I greatly appreciate it.
Hello There,I'm trying to create a view that has calculations dependent oncalculations, where the problem resides is that each time I make acalculation I must create an intermediate view so I can reference aprevious calculation.for example lets say I have my_table that has columns a & b. now I wanta view that has a & b, c = a + b, and d = c + 1.this is grossly simplified, the calculations I actually use are fairlycomplex and copying / pasting them is out of the question.so what I have is my_view_a which makes column c, and my my_view_finalwhich makes column d (however, in my real application I have 5 of theseviews, a/b/c/d/e/)is there anyway I can consolidate all these views into one? I wasthinking of using a stored procedure with temp tables or somethingalong those lines.I just which I can use the aliases that I create for c in d in onestep.any insight would be greatly appreciated.
Hi everyone, I have a problem like this . I have tables Coursegroupcode, which has groupname, codeI have Courses That has Coursename, its code(group code),Term, Course Number Enrollment table which has Foreign keys Term,Course NUmber , SSN I need to get a view like thisI should list all the coursecodes and people enrolled for each course code for selected terms Course Table Primary keys(TERM,COUSE Number)Enrollment Table(Foreign keys) TERM ,COURSE NUMBER, SSNplease help
Can SQL Views insert to the tables they are created from?I have a database that is not well structured. It has a lot of redundency. What I want to do is create a SQL View that brings in all the data I need and have my application use that new SQL View instead of the data tables. Then I want to be able to insert new information to a SQL View that actualy gets inserted into the tables that the SQL Viewer is created from.Can this be done?Does this make any sense?
I have a query which unions the four select statements.....
the select statements are joined with other tables and views.....
When i execute the query i get ODBC timeout error........
But the strange thing is that if i execute the view individually once and again execute the query it works fine.......and later it justs works fine....
Is there a performance hit running a sp against a view versus a base table. The view just excludes several of the records based on some criteria, and all the data I will be retreiving is included in the view. Or should I just stick my criteria in the SP to exclude the data?
I have some rather complex views to work with. Do they slow you down? Would it be better to move a view into a stored procedure? Is there any difference between these three solutions? <B>1.Using views:</b> Create view X As Select Col1, Col2, Col3 from Table1 Go
Create view Y As Select Col1, Col2, Col3 from Table2 Go
Create proc Z As Select X.Col2, X.Col3, Y.Col2, Y.Col3 From X inner join Y on X.Col1 = X.Col1 GO
<b>2.Using just a stored proc:</b> Create proc Z As Select X.Col2, X.Col3, Y.Col2, Y.Col3 from (Select Col1, Col2, Col3 from Table1) X inner join (Select Col1, Col2, Col3 from Table2) Y on X.Col1 = Y.Col1
<b>.Joining tables</b> Create proc Z As Select Table1.Col2, Table1.Col3, Table2.Col1, Table2.Col3 FromTable1 inner join Table2 on Table1.Col1 = Table2.Col1 go
My First time building "Views" in SQL...... I'm trying to figure out how to return a 1 instead of a two when I Count the number of records that return inn my view.
here's what I have;
SELECT TOP 100 PERCENT dbo.tbl_ProcTimesheet.idCalendar, dbo.tbl_ProcTimesheet.erNum, dbo.tbl_ProcTimesheet.PayDate, COUNT(dbo.tbl_ProcTimesheet.TransAmt) AS Shifts, dbo.tbl_ProcTimesheet.[Employee Number], dbo.tbl_ProcTimesheet.YCode, dbo.tbl_ProcTimesheet.XCode, dbo.tbl_ProcTimesheet.ZCode, dbo.tbl_ProcTimesheet.eeLink FROM dbo.tbl_ProcTimesheet INNER JOIN dbo.tbl_SysVarRestEeShiftPayCodes ON dbo.tbl_ProcTimesheet.Code = dbo.tbl_SysVarRestEeShiftPayCodes.PayCode INNER JOIN dbo.tbl_SysVarRestEeShiftRules ON dbo.tbl_ProcTimesheet.YCode = dbo.tbl_SysVarRestEeShiftRules.YCode AND dbo.tbl_ProcTimesheet.ZCode = dbo.tbl_SysVarRestEeShiftRules.ZCode WHERE (dbo.tbl_ProcTimesheet.Sequence <> N'0') AND (dbo.tbl_ProcTimesheet.Week < 3) GROUP BY dbo.tbl_ProcTimesheet.idCalendar, dbo.tbl_ProcTimesheet.erNum, dbo.tbl_ProcTimesheet.PayDate, dbo.tbl_ProcTimesheet.TransAmt, dbo.tbl_SysVarRestEeShiftRules.DailyHours, dbo.tbl_ProcTimesheet.[Employee Number], dbo.tbl_ProcTimesheet.XCode, dbo.tbl_ProcTimesheet.YCode, dbo.tbl_ProcTimesheet.ZCode, dbo.tbl_ProcTimesheet.eeLink, dbo.tbl_ProcTimesheet.Sequence, dbo.tbl_ProcTimesheet.Week HAVING ('IIf' > '1,1,0') AND ('if' > '1,1') AND (dbo.tbl_ProcTimesheet.erNum LIKE N'5648 ') AND (SUM(dbo.tbl_ProcTimesheet.TransAmt) >= dbo.tbl_SysVarRestEeShiftRules.DailyHours) AND (dbo.tbl_ProcTimesheet.PayDate = CONVERT(DATETIME, '2005-09-06 00:00:00', 102)) AND (COUNT(dbo.tbl_ProcTimesheet.TransAmt) > 0) ORDER BY dbo.tbl_ProcTimesheet.PayDate, dbo.tbl_ProcTimesheet.[Employee Number]
When it counts shifts, I only want to return a maximum of i, as in either you had a shoft that day, or not. I gave up trying to figure out the "IF" string in SQL, and for the life of me I can not figure this out.
Does anyone know a good site with info on how to construct an SQL View?
MS SQL Server 2005 What happens if 2 or more users use the same View at the same time or while other user is using it, normally from Crystal Reports 10 It rebuilds again? Or?
I have a SQL server: SQL2005 2 databases db_repl: contains replicated data from mainframe db_my: my database
db_my.vwRepl_WorkOrders: Select 1,2,3 from db_repl.dbo.WorkOrders db_my.vwWorkOrdersOpen: select 1,2,3 from db_my.vwRepl_WorkOrders where Status=1
which approach is more optimal to do in order to find open work orders from Texas: [select 1,2,3 from db_repl.dbo.workOrders where Status=1 and State=1 or select 1,2,3 from db_my.vwWorkOrdersOpenwhere where State=1] Is it best practice to use 'subviews' that acess db_repl directly or acess views in my_db that acess db_repl (2 hops or more if views call other views that call other views) Answer might be the obvious but just in case I am missing something here . Thanks!