Wednesday, June 15, 2011

How to share data between stored procedures ? Good Link

http://www.sommarskog.se/share_data.html



Discusses in detail various methods :

This table summarises the methods:

MethodInput/ OutputSQL Server versionsComment
Using OUTPUT ParametersOutputAllNot generally applicable, but sometimes overlooked.
Table-valued FunctionsOutputSQL 2000Probably the best method for output, but has some restrictions.
Inline FunctionsUse this when you want to reuse a single SELECT.
Multi-statement FunctionsWhen you need to encapsulate more complex logic.
Using a TableIn/OutAllMost general methods with no restrictions, but a little more complex to use.
Sharing a Temp TableMainly for single pair of caller/callee.
Process-keyed TableBest choice for many callers to same callee.
Global Temp TablesA variation of Process-Keyed.
INSERT-EXECOutputSQL 6.5Does not require rewrite. Has some gotchas.
Table Parameters and Table TypesIn/(Out)SQL 2008Could have been the final answer, but due to a restriction it is only mildly useful in this context.
Using the CLROutputSQL 2005Does not require a rewrite. Clunky, but is useful as a last resort when INSERT-EXEC does not work.
OPENQUERYOutputSQL 7Does not require rewrite. Tricky with many pitfalls.
Using XMLIn/OutSQL 2005A roundabout way that requires you to make a rewrite, but it has some advantages over the other methods.
Using Cursor VariablesOutputSQL 7Not recommendable.




Have a look at other articles also on the home page :

http://www.sommarskog.se/index.html


  • The curse and blessings of dynamic SQL. How you use dynamic SQL, when you should - and when you should not. (Added June 2001, Completely revised: April 2006. Most recent update: December 2008.) German translations of an old version available.

  • Arrays and Lists in SQL Server. Several methods on how to pass an array of values from a client to SQL Server, and performance data about the methods. There are three articles: one for SQL 2008 which is devoted entirely to table-valued parameters, one for SQL 2005 and later (for situations when you cannot use TVPs), and one for SQL 2000 and earlier. (Added March 2003. Version for SQL 2005 added March 2007, major revision and addition of article for SQL 2008: January 2010. Most recent update October 2010.)

  • Dynamic Search Conditions. How to write a stored procedure that permits users to select among many search conditions, using both dynamic and static SQL. There are two articles: one for SQL 2008, and one for SQL 2005 and earlier. (Added May 2003. Most recent update: November 2009. French and German translations of the SQL 2005 version available.)

  • How to share data between stored procedures. Different ways of passing sets of data between stored procedures. (Added July 2001, Most recent update: January 2010)

  • Giving Permissions through Stored Procedures. SQL 2005 adds two new methods – signing with certificates and impersonation with EXECUTE AS – that can manage cases where the classic method of ownership chaining fails. I explain the two new methods, as well as the old one, and warn you about the pitfalls. (Added February 2006)

  • Slow in the Application, Fast in SSMS? Ever experienced the situation that your query runs slow in your application, but when you try it in SQL Server Management Studio, the response is immediate? This article explains why this may happen, what parameter sniffing is and suggests methods to analyse and address your performance problem. (Added February 2011.)

  • Build Your Own Index. This is a teaser for.. Well, follow the link and read on! (Added November 2009.)

  • Error Handling in SQL 2005 and Later. This is an unfinished article, which consists only of a jumpstart section that shows how to use BEGIN-TRY. The full article will appear ...sometime. The article also gives you links to my old articles for error handling in SQL 2000. (Added July 2003. Most recent update: November 2009 )

  • Connecting to MS SQL Server from Unix. A brief summary of what's available. Since I don't work with Unix myself, it's not extremely well-informed, but people have kept asking me about this. (Added Jan 1999, Latest update: June 2008)


  • How find the difference between two consecutive rows in a table ?

    By using a self join , as below.
    Note that this query assumes the table has an autoincrement column like ID . If it is not, then possibly it will have to generated using ROW_NUMBER. Note that using autoincrement is better than using something like TOP.



    create
    table test ( ID int, val int) insert test( ID , val) select 1, 10union all select 2, 12union all select 3, 9union all select 4, 15union all select 5, 8union all select

     select



    select

    ID val
    1 10
    2 12
    3 9
    4 15
    5 8
    6 20




    select a.ID , b.ID , a.val , b.val , a.val - b.val as diff from test a left join test b on


    ID ID val val diff
    1 NULL 10 NULL NULL
    2 1 12 10 2
    3 2 9 12 -3
    4 3 15 9 6
    5 4 8 15 -7
    6 5 20 8 12
    b.ID = a.ID -1
    * from test
    a.ID , b.ID , a.val , b.val , a.val - b.val as diff from test a left join test b on b.ID = a.ID -1
    6, 20

    Monday, June 13, 2011

    Very good articles on SQL Server

    Cumulative Sum in SQL Server

    http://stackoverflow.com/questions/2120544/how-to-get-cumulative-sum


    declare  @t table (
        id int
    ,
        SomeNumt int 
    )



     
    insert into @t select 1,10 union select 2,12 union select 3,3 union select 4,15 union select 5,23

     
    select * from @t 




    select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum from @t t1 inner join @t t2 on t1.id >= t2.id group by t1.id, t1.SomeNumt order by t1.id 

    What is recommended practice for Response.Redirect ?

    Use Response.Redirect ( URL , false);

    The second parameter false is important, it avoids calling the End method  of calling page.


    Response.Redirect uses HTTP 302 status code (Moved Temporarily) in the background.




    http://msdn.microsoft.com/en-us/library/a8wa7sdt.aspx

    When you use this method in a page handler to terminate a request for one page and start a new request for another page, set endResponse to false and then call the CompleteRequest() method. If you specify true for the endResponse parameter, this method calls the End method for the original request, which throws a ThreadAbortException exception when it completes. This exception has a detrimental effect on Web application performance, which is why passing false for the endResponse parameter is recommended. For more information, see the End method.

    ASP.NET performs the redirection by returning a 302 HTTP status code.



    http://support.microsoft.com/kb/312629/EN-US/

    PRB: ThreadAbortException Occurs If You Use Response.End, Response.Redirect, or Server.Transfer

    [GENERATING PAGE NUMBER IN SELECT QUERIES] I want to give a group number to rows, which will change on every 10th row. e.g. First 10 rows grp no 0, next 10 rows grp no 1, and so on. How can I do this ?



    Give Row number to each row ( by using ROW_NUMBER)  , then divide this row_number by a particular no  say 10 to get grp no.


    ( Note that this 10 can be any number , it will be the group or page size)
    select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum , row_number() over ( order by timeparts ) /10 as grp from RSCFuelChart
    e.g


    fuellevelDIFFtimepartsrownumgrp
    1.0207:20:1710
    1.0207:20:5020
    1.0207:21:2030
    1.0207:21:5040
    1.0207:22:2050
    1.0207:22:5060
    1.0207:23:2070
    1.0207:23:5080
    1.0207:24:1890
    1.0207:24:51101
    1.0207:25:21111
    1.0207:25:51121
    1.0207:26:21131
    1.0207:26:51141
    1.0207:27:21151
    1.0207:27:48161
    1.0207:28:22171
    1.0207:28:52181
    1.0207:29:22191
    1.0207:29:52202
    1.0207:30:22212
    1.0207:30:52222
    1.0207:30:52232
    1.0207:31:22242
    1.0207:31:53252
    1.0207:32:23262
    1.0207:32:53272
    1.0207:33:23282
    1.0207:33:53292
    1.0207:34:23303
    1.0207:34:53313
    1.0207:35:21323
    1.0207:35:54333
    1.0207:36:24343




    This can be very useful in smoothning the graphs, e.g. following query takes avg of every 10 records and max(time) of these 10 records.

    select AVG(a.fuellevel) as fiellevel, MAX(timeparts) as timeparts from ( select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum , row_number() over ( order by timeparts ) /10 as grp from RSCFuelChart ) A group

    this essentially reduces no of recs from say 500 in all to 50 only to be plotted.
    by a.grp


    ==============================
    So to summarize :


    declare @pagesize int = 20 , @pagenumber int = 1select * from ( select fuellevel , DIFF , timeparts , row_number() over ( order by timeparts ) as rownum from RSCFuelChart ) A where ((a.rownum /@pagesize) + 1) = @pagenumber

    Friday, June 3, 2011

    Must set culture value for using CompareValidator for date formatting.




    culture en-US : M/dd/yyyy
    culture en-GB : dd/MM/yyyy , dd-MM-yyyy , dd.MM.yyyy


    must set
    System.Threading.Thread.CurrentThread.CurrentCulture

    not
    System.Threading.Thread.CurrentThread.CurrentUICulture



    System.Threading.
    System.Threading.
    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("en-GB");Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");


    http://msdn.microsoft.com/en-us/goglobal/bb688124


    The easiest and most efficient way of formatting dates in the .NET world is to take advantage of the DateTime structure, which provides methods allowing you to perform culture-sensitive operations on DateTime. Use the DateTimeFormatInfo class to format and display a DateTime based on a culture-specific standard. DateTimeFormatInfo defines how DateTime values are formatted and displayed, depending on the culture. For example, using the ShortDatePattern, the date April 24, 2001, is formatted as 4/24/2001 for the "en-US" culture and 24/04/2001 for the "en-GB"-English (United Kingdom)-culture.
    An instance of DateTimeFormatInfo can be created for a specific culture, but not for a neutral culture. A neutral culture does not provide enough information to display the correct date format. You just saw that English (United States) and English (United Kingdom) share the same neutral culture-English-and yet have completely different formatting for the date.

    Comparing features of LangGraph vs CrewAI

    LangGraph Orchestration vs CrewAI Orchestration The following table compares LangGraph and CrewAI orchestration across their menta...