Queries, Views, Procedures : Best Practice

Nov 23, 2007

Hello all,

I'm looking for a best practice.
Let's say you have a report that contains out of 3 queries.

How are you going to create the report and why?

1. Just use "select * from table where p1= value" in your report
2. Save the query in your db as a view and use the view in your report
3. Create a procedure that contains all 3 queries. And use the procedure with some parameters in your report?

Kr
Karel.

View 5 Replies


ADVERTISEMENT

Best Practice For Indexed Views And Aggregates Tables

Apr 10, 2007

Hi, all experts here,

Thank you very much for your kind attention.

I am having some questions on indexed views and aggregate tables.

My question is: To improve the performance of the queries, is it better to use indexted views or aggregates tables for those aggregates which are often queried?

I am looking forward to hearing from you.

Thank you very much in advance for your help.

With best regards,

Yours sincerely,

View 6 Replies View Related

Best Practice For A Long Running Queries / Asynchronously Calling A Stored Proc?

May 23, 2008

All -

I am using SQL Server 2005 and I have an endpoint that exposes some stored procedures as web-methods in the endpoint.

One particular stored procedure I have exposed takes a long time to execute: about 10 - 15 minutes. While, it is OK, that this stored procedure takes this long, it is not desirable for the HTTP Request that executed this proc to not wait for that long.

What I want to be able to do is to call the stored procedure and have the call return immidetaly but the stored proc continues what its doing. I will call another stored proc at a later time to retrive the result of the first stored proc. The first proc will store its results in a temp table. I am thinking of using SQL Server Service Broker to achieve this.

Is there a better a way to achieve this? And how does SQL Server process the Service Broker requests, i.e., I dont want the query to be executed when the server is busy. Are there any hints that I need to give to Service Broker to be able to do this?

Thanks.

View 5 Replies View Related

Stored Procedures -- Correct Practice?

Jan 16, 2008

Hi,

I have 3 tables: authors, companies, and countries.

I have a stored procedure defined as:

PROC addAuthor(AuthorName, CompanyID, CountryID)
INSERT INTO authors (author_name, comp_id, country_id) VALUES( authorName, CompanyID, CountryID)
END PROC

....So the ID of the company and the country are the parameters. This makes it easy since I can just do a direct INSERT statement into the authors table.

Would it be better practice if the parameters asked for the name (not the ID's) of the company and country instead? This way we do not have to memorize the ID's for everything. So for example

PROC addAuthor(AuthorName, CompanyName, CountryName)
SELECT COUNT(*) FROM companies INTO v_numRows WHERE companies.company_name = CompanyName
IF v_numRows = 0
-- error
END IF

SELECT COUNT(*) FROM countries INTO v_numRows WHERE countries.country_name = CountryName
IF v_numRows = 0
-- error
END IF
END PROC

yeah, I might have the SQL syntax wrong up there (not entirely sure how it works for variables) but the gist of it is that I will need to include validation code within the stored procedure.

What do you guys think of this? Which method do you prefer? Is there a significant performance decrease with using the second method?

Thanks for any insights

View 8 Replies View Related

Best Practice: Procedures: (Insert And Update) OR JUST (Save)

Aug 18, 2007

I have a Product Table.
And now I have to create its Stored Procedures.
I am asking the best practice regarding the methods Insert And Update.
There are two options.
1. Create separate 2 procedures like InsertProduct and UpdateProduct.
2. Create just 1 procedure like ModifyProduct. In which programmatically check that either the record is present or not. If present then update and if not then insert. Just like Imar has done in his article http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=419
Can any one explain the better one.
Waiting for helpful replies.
http://imar.spaanjaars.com/QuickDocId.aspx?quickdoc=419
a

View 3 Replies View Related

Views Vs Queries

Jul 20, 2005

Hello:I am new to T-SQL programing, and relativly new to SQL statements ingeneral, although I have a good understanding of database theory. I'm alittle confused as to the fundamental differences between a view and aquery, or rather, when it is more appropriate to use one vs. the other.It seems to me that most select queries can be implemented as views, and Ican't see the downside to doing so.I have a rather complicated database structure in terms of the relationshipsbetween various tables, and to get the information that I need into the userinterface forms I have to use a number of nested queries (first to combinetables together that have been normalized to place the related info in asingle table, then next to extract only the info that I require. In somecases I would need a 3 level deep nested query to get to what I need. Byimplementing the first step, that of combing the normalized tables back intoa single table as a view, it seems to me that I've completed the first stepat the database level, thus making my queries or stored procedures easier toimplement.I would appreciate any comments which can help me to decide when it is or isnot appropriate to use a view in these circumstances. I should point outthat I don't plan on using the views as part of action queries since theyare made up of multiple tables, I'm primarily interested in using them tooutput data which has been consiolidated from multiple tables.Thanks in advance to any and all who take the time to reply.

View 8 Replies View Related

Best Practice - Data Sources And Data Views Vs OLE DB Source

Feb 26, 2008

Hi, i'm wondering which is the best way to search data in a SQL Server.
I reach data using Data Sources and Data Views and also with OLE DB Source with a Data access mode: Named query.
I have to write the data into a Flat File. So, does any one knows which is the best practice for this? Or any one of the two are good choices?
Thanks for your help.

Beli

View 6 Replies View Related

Print List Of Queries, Tables, Views And Sp

Oct 29, 2007

I just started a new job and 1st time on sql server, how can i print list of queries, tables, views, stored procedures and functions?

View 6 Replies View Related

SQL 2012 :: Converting Access Queries To Views

May 13, 2014

Is there an easy way to convert Access Queries to SQL Views without doing it manually?I have used the Databse tool to migrate tables, but cannot see to find something similiar for queries.

View 3 Replies View Related

Best Practice To Pull Data From Sql Server 2000 To Sql Server 2005 With Dynamic Queries

May 3, 2007

Hi There,

I need to pull data using input from one table in sql server 2005. I have to query against the sql server 2000 database and pull data into sql server 2005. I have a list of ids that I have to pass to a query to get the desired data. What is the best practice for this. Can I use SSIS or do I need to build an app in C#? Can somebody please reply back?

Thanks a lot!!

View 4 Replies View Related

Views Vs. Standard Procedures

Jan 31, 2007

Hello,
I have a question where the answer may be simply opinion, or there may be strong reasons.  I guess that's why I'm asking the question... 
The question is - when should one create a view, and when should one use a stored procedure?  Obviously, when doing multiple queries or passing parameters, you must use a SP.  I'm talking about the case of a single query perhaps joins a couple of tables.
 
Hope that's not a stupid question...
 
Thanks in advance,
Paul

View 4 Replies View Related

Using Views Within Stored Procedures

Jul 22, 2007

Good evening:Can anyone give me the low-down on using view from within my stored procedures?  I've got some rather complex views that join multiple tables resulting in a staggering SQL query.  I then use stored procedures to accept input parameters to determine order by and direction clauses.  This results in some rather large "IF ELSE IF ELSE IF..." statements in my SP.  Put a complicated SQL query together with lots of IF THEN and it becomes difficult to keep stuff straight in the SP.  Therefore, using the view to keep my SQL in one place is an ideal solution. However, and here's the question, does it have a negative impact on speed and/or scalability.  We plan to use these SP's within an ASP.NET website that may get a very large amount of traffic. Any advice/help you can give me would be really appreciated. Thanks!
- Future Daddy
 

View 2 Replies View Related

Getting At Views And Stored Procedures

Feb 15, 2001

I have been having troubling getting at views and stored proceduers with ADO objects while developing in VB.

I can get a list of the views and stored procedures, but I don't know how to actually get the text values of them... i would like the text so that I can parse/compare them agianst one another...

Any thoughts or help on this?

I would really appreciate the help!

Matt

View 2 Replies View Related

Views In Stored Procedures?

Feb 16, 2007

Hello,

This maybe a simple question, I'm using reporting services to make a report, question is can I write a stored procedure that queries from a join of two views instead of two tables?

I'm having trouble getting my table data into the reportable state I want it, and I think organising into views might be easier.

Thanks for any help.

View 3 Replies View Related

Views Inside Stored Procedures.

Apr 18, 2007

Hi everybody,I am trying to alter a view inside a Stored Procedure and sql server is not allowing me to do so.Can we not have an alter view inside a Stored Procedure?Here is the code: CREATE PROCEDURE dbo.[UPDATE_VIEW_LINEITEMALLOTMENT]     -- Add the parameters for the stored procedure here@MAINID int,@BE VARCHAR(8)ASBEGIN    -- SET NOCOUNT ON added to prevent extra result sets from    -- interfering with SELECT statements.    --SET NOCOUNT ON;        ALTER VIEW [Budget_Management_System].[dbo].[LineItem_OCAFund]        AS        SELECT     TOP (100) PERCENT SUM(dbo.ALL_OCAFUND.AMOUNT) AS Expr2, dbo.APP_LINEITEM.LINENUM, dbo.APP_LINEITEM.PC,                               dbo.APP_LINEITEM.CATEGORY, dbo.APP_LINEITEM.ROWID, dbo.APP_LINEITEM.MAINID        FROM         dbo.APP_LINEITEM INNER JOIN                              dbo.ALL_ORG ON dbo.APP_LINEITEM.ROWID = dbo.ALL_ORG.LINEITEMID INNER JOIN                              dbo.ALL_OCAFUND ON dbo.ALL_ORG.ROWID = dbo.ALL_OCAFUND.ORGID INNER JOIN                              dbo.APP_AMENDMENT ON dbo.ALL_ORG.AMENDMENTID = dbo.APP_AMENDMENT.ROWID AND                               dbo.ALL_OCAFUND.AMENDMENTID = dbo.APP_AMENDMENT.ROWID        GROUP BY dbo.APP_LINEITEM.LINEORDER, dbo.APP_LINEITEM.BE, dbo.APP_LINEITEM.MAINID, dbo.APP_LINEITEM.LINENUM, dbo.APP_LINEITEM.PC,                               dbo.APP_LINEITEM.CATEGORY, dbo.APP_LINEITEM.ROWID, dbo.APP_AMENDMENT.AMENDMENTORDER        HAVING      (dbo.APP_LINEITEM.BE = @BE) AND (dbo.APP_LINEITEM.MAINID = @MAINID) AND (dbo.APP_AMENDMENT.AMENDMENTORDER = 1)        ORDER BY dbo.APP_LINEITEM.LINEORDER   ENDthanks a lot,Murthy here 

View 7 Replies View Related

Accessing Views From Stored Procedures

Sep 7, 2007

Hi,I was just wondering if it's possible to access views from stored procedures? I know it doesn't make much sense, but would it be possible? If so, can you also give me some code example?Thanks. 

View 4 Replies View Related

Is There Any Point In Views? Vs Stored Procedures?

Nov 8, 2007

I do all my data access through stored procedures. Is there any benefit in creating views that the stored procedure accesses. At the moment, I tend to just write the select and join within the stored procedure.

View 12 Replies View Related

Scripting Views && Stored Procedures

Oct 9, 2002

Is there a way to script views and/or stored procedures from Query Analyzer? I am hoping to do this as part of a stored procedure.

Thanks!

View 3 Replies View Related

Transfer Stored Procedures And Views Only

Oct 19, 2001

I have two databases named A and B. They are exactly same table structure, but the records are different, and we have near 400 stored procedures and over100 views in the database A. Now my question is that how can I transfer all stored procedures and views from database A to database B?

Thank you very much!

View 2 Replies View Related

Views / Stored Procedures / Functions

Dec 1, 2005

Hi All,

Novice question. Would someone explain tell me what a view is used for? Also I am confused about the difference between a function and a stored procedure. They both seem like functions to me.

View 7 Replies View Related

Question Regarding Stored Procedures, Views And ASP

Jul 20, 2005

Please could someone explain to me the differences between a storedprocedure and a view.The reason for this question is I have two almost identical ASP pages.Both get the same results but one uses a stored procedure and one usesa view. If the query returns no results the 'view page' generateserrors and therefore I have to check for BOF and EOF, whereas the'stored procedure page' does not generate errors and instead wouldappear to return a recordset with 0 entries.Thanks in advanceNeil.

View 1 Replies View Related

Can't Import Views And Store Procedures

Aug 6, 2007

I recently set up a new sql 2005 standard edition and planning to mirgrate our production sql 2000 data. but only the tables were migrated from the copy data or import / export task. I cannot find a way to recreate the store procedures and views on the new server without create one view and one store procedure at a time unless all hundreds of views and procedures were rescripted. can anyone help

Thank

Andy Wong
awong@virginiadare.com

View 3 Replies View Related

Diffrence Between Views And Stored Procedures

Jun 18, 2007

Hi

what is the diffrence between views and stored procedures in sqlserver 2005

thanks in advance.

View 3 Replies View Related

Create Views In Stored Procedures?

Sep 25, 2007

Can you create views within a stored procedure?

View 10 Replies View Related

MS Access And Stored Procedures Or Views

Mar 13, 2008

Can Microsoft Access access a stored procedure on the SQL Server? If not, is there any way to assign a parameter to a view? I can link to a view in Access, but I cannot link to a procedure. I need to SELECT * from aTable WHERE ID = [@PassidInID]. Any way to do this through Access? The reason for this, is because I am trying to run an existing query (which is very busy - using other queries with queries in them etc.), and I keep getting an ODBC error. I am thinking that if I move the queries to the SQL Server, it may get rid of this error?
It's a 2-tier app I have. Tables on the server, forms, queries, and everything else in Access.
Thanks in advance - I hope.

View 9 Replies View Related

Performance Problem With Views And Stored Procedures..

Nov 8, 2000

Hello Everybody,

I posted this same question couple of times in the news groups but no answers. I have a 2 tables and i am doing a union query using a view. each has 250 rows. The query takes 20 seconds to return the results. no joins or anything. the create view simply looks like this:

create view myview as
select id, name from table1
union
select id,name from table2

Where as if i write a stored procedure like below, it returns the rows in 4 seconds.
create table #mytable
( id int, name varchar(30))
insert into #mytable (id, name) select id, name from table1
insert into #mytable (id, name) select id, name from table2
select id,name from #mytable.


I prefer doing in the view since both returns the same result. I tried running dbcc, update statistics. but no luck. Can anyone please help me in this issue.

Thanks
Ramesh

View 3 Replies View Related

Permissions To Change Table, Views And Procedures

May 20, 2008

Hi

What permissions do I need to set so that a user can change tables, views and procedures?

View 7 Replies View Related

Views Vs Complex Joins In Stored Procedures

Oct 17, 2007

I have a multiple table dataset that needs to be returned, with at least 5 joins, some inner, some left outer.

Currently, this is done via a parameterized stored prodedure, which is used fairly frequently. The parameters only affect the where clause, not the joins.

Would it be better to create the view with the joins already done, then pass in the parameters with the stored procedure? Which is better for overall performance? Which is better for quicker response times to the calling asp.net application?

Thanks in advance,

Leah

View 2 Replies View Related

Views / Stored Procedures / Updating Records

Jul 23, 2005

This is a general question regarding the use of view and storedprocedures. I'm fairly new to databases and SQL.I've created a SQL database using an Access Data Project ("ADP") andI'm satified with the table structure. I've moved on to building somefront ends for our users.I'm running into situations where I want subreports to be built fromqueries [views or stored procedures-I don't know which to use so I usethe term query] that are dependent on the values in other controls.I've played with stored procedures and I've figured out how to sendcriteria to a stored procedure and then dynamically change the recordsource of a subreport.However, I'm running into cases where I can't add records to theresults of a stored procedure. The table I'm running a stored procedureon has five fields: (1) Primary Key for each record, (2) FundID that'sa primary key in another table, (3) CompanyID that's a primary key inanother table, (4) Attribute 1 of the (Fund/Company) and (5) Attribute2 of the (Fund/Company).The stored procedure filters the set of Fund/Companies based on aFundID from a form. I can update this stored procedure. However, forusers, they would like to see the Fund Name from the table that hasunique FundIDs. As soon as I include that into the stored procedure, Ican no longer add records.My questions are many:1. Is there a primer online that discusses the theory behind myquestion? Recordsets, updatability, working with recordsets in forms?2. What are some best practices for developing subreports, combo boxes,list boxes, etc. where the data is dependent on the values in a control3. I'm struggling with the best ways to grab objects on a form. If I'mon the main form I'm comfortable working with theMe.__object__.__sub-oject routine. However, if I'm in one subform whereI need another subform to change based on the record I'm in, I feelthat my code to get at the subform is very klunky..forms.main form name.sub form name.form.record sourceI don't even know how I figured out the "form" part before recordsource. Again, are there some basic rules or guides about navigatingthrough forms in VBA?4. Should I be developing front ends in some other environment?I know it's a lot, but all the advice from the newsgroups seems topresuppose some knowledge about how ADP, ADO, ODBC..blah blah and Ican't seem to find any documents about ADP and SQL.

View 3 Replies View Related

Views Vs Stored Procedures, Whats The Difference?

Jan 27, 2006

The purpose for my questions is accessing these technologies fromapplications. I develop both applications and databases. Working withMicrosoft C#.NET and Microsoft SQL Server 2000 Production and 2005 TestEnvironments.What is the purpose of a view if I can just copy the vode from a viewand put it into a stored procedure?Should I be accessing views from stored procedures?Should I use views to get information? and Stored Procedures forInserts, Updates and Deletes?What are the performance differences between the two?Thank you for any and all information.SBProgrammer

View 28 Replies View Related

Saving Procedures And Views To Remote Server

Apr 13, 2007

I am using SQL Server Management Studio and Visual Studio 2005 and I am a newbie.
Once connected to a remote server and database I am able to use SQL Server Management Studio to modify a table and save it. When I modify a stored procedure or create a new one and attempt to save it I get my local dialog box to save to my system with a new name €œSQLQuery6.sql.€? How do I cause the procedure to be saved to the remote database? If anyone can help this or where I can find it I will appreciate it. Thanks in advance.

View 4 Replies View Related

Problematic Stored Procedures Using Views With Triggers

May 9, 2006

Hi,

I would like to use the view of the company data in the following stored procedures

without needing to pass a value into the CompanyID identity column. Is there a way to do this? The following code contains the view, the insert trigger on the view, and the desired stored procedures.

Also, this code is given me problem as well the error message says it cannot convert varchar to int. This code is located in the insert trigger.

INSERT INTO State(StateName)
VALUES(@StateName)

Here is the code of the company view, notice the C.CompanyID it is used in the GetCompany stored procedure.

SELECT C.CompanyID, C.CompanyName, A.Street, A.City, S.StateName, A.ZipCode, A.Country, P.PhoneNumber


FROM Company AS C INNER JOIN
Address AS A ON C.AddressID = A.AddressID INNER JOIN
State AS S ON A.StateID = S.StateID INNER JOIN
Phone AS P ON C.PhoneID = P.PhoneID

Here is code for the insert trigger on the view

CREATE TRIGGER InsertTriggerOnCustomerView
ON ViewOfCompany
INSTEAD OF INSERT
AS
BEGIN

DECLARE @CompanyName VARCHAR(128)
DECLARE @AddressID INT
DECLARE @Street VARCHAR(256)
DECLARE @City VARCHAR(128)
DECLARE @StateID INT
DECLARE @StateName VARCHAR(128)
DECLARE @ZipCode INT
DECLARE @Country VARCHAR(128)
DECLARE @PhoneID INT
DECLARE @PhoneNumber VARCHAR(32)
--DECLARE @CompanyID INT

SELECT
--@CompanyID = CompanyID,
@CompanyName = CompanyName,
@Street = Street,
@City = City,
@StateName = StateName,
@ZipCode = ZipCode,
@Country = Country,
@PhoneNumber = PhoneNumber
FROM INSERTED

INSERT INTO State(StateName)
VALUES(@StateName)

SET @StateID = @@IDENTITY

INSERT INTO Address(Street, City, StateID, Country, ZipCode)
VALUES(@Street, @City, @StateID, @ZipCode, @Country)

SET @AddressID = SCOPE_IDENTITY()

INSERT INTO Phone(PhoneNumber)
VALUES(@PhoneNumber)

SET @PhoneID = SCOPE_IDENTITY()

INSERT INTO Company(CompanyName, AddressID, PhoneID)
VALUES(@CompanyName, @AddressID, @PhoneID)

END

The stored procedures are here.

CREATE PROCEDURE GetCompany
(
@CompanyID INT
)
AS
BEGIN
SELECT CompanyName,Street, City, StateName,
ZipCode, Country, PhoneNumber
FROM
ViewOfCompany
WHERE
CompanyID = @CompanyID
END
GO

CREATE PROCEDURE AddCompany
(
@CompanyName VARCHAR(128),
@Street VARCHAR(256),
@City VARCHAR(128),
@StateName VARCHAR(128),
@ZipCode VARCHAR(128),
@Country VARCHAR(128),
@PhoneNumber VARCHAR(32)
)
AS
BEGIN
INSERT INTO ViewOfCompany
VALUES(@CompanyName, @Street, @City, @StateName,
@ZipCode, @Country, @PhoneNumber)
END
GO

View 3 Replies View Related

Stored Procedures, Queries Etc

Sep 19, 2007

Hi,
 I have a quick question about pushing things onto the database to do.  It is supposed to be more efficient, and it is more secure.  The downside as far as I can tell is that one can end up with incredibly expensive and complex database deployment using costly servers and software. 
Is there an approach that minimizes database query time by moving much of the processing to the (less efficient) web server calling the database, and then only using simply queries.  One would then process the data in code, stripping it down to exactly what is needed, rather than doing complex things on the db itself.
An example of this might be that you want to parse a field on a particular char. and return only the first part of the string, up until this char occurs.  This can be done on the db server, but...
I understand that one might return extra information, but it seems there is a large potential payoff in less complexity and expense.
Anyway, if you know of such an approach and the name of it, I would appreciate hearing it,
Thanks

View 1 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved