Search Results

Search found 9396 results on 376 pages for 'stored procedures'.

Page 21/376 | < Previous Page | 17 18 19 20 21 22 23 24 25 26 27 28  | Next Page >

  • Returning Identity Value in SQL Server: @@IDENTITY Vs SCOPE_IDENTITY Vs IDENT_CURRENT

    - by Arefin Ali
    We have some common misconceptions on returning the last inserted identity value from tables. To return the last inserted identity value we have options to use @@IDENTITY or SCOPE_IDENTITY or IDENT_CURRENT function depending on the requirement but it will be a real mess if anybody uses anyone of these functions without knowing exact purpose. So here I want to share my thoughts on this. @@IDENTITY, SCOPE_IDENTITY and IDENT_CURRENT are almost similar functions in terms of returning identity value. They all return values that are inserted into an identity column. Earlier in SQL Server 7 we used to use @@IDENTITY to return the last inserted identity value because those days we don’t have functions like SCOPE_IDENTITY or IDENT_CURRENT but now we have these three functions. So let’s check out which one responsible for what. IDENT_CURRENT returns the last inserted identity value in a particular table. It never depends on a connection or the scope of the insert statement. IDENT_CURRENT function takes a table name as parameter. Here is the syntax to get the last inserted identity value in a particular table using IDENT_CURRENT function. SELECT IDENT_CURRENT('Employee') Both the @@IDENTITY and SCOPE_IDENTITY return the last inserted identity value created in any table in the current session. But there is little difference between these two i.e. SCOPE_IDENTITY returns value inserted only within the current scope whereas @@IDENTITY is not limited to any particular scope. Here are the syntaxes to get the last inserted identity value using these functions SELECT @@IDENTITY SELECT SCOPE_IDENTITY() Now let’s have a look at the following example. Suppose I have two tables called Employee and EmployeeLog. CREATE TABLE Employee ( EmpId NUMERIC(18, 0) IDENTITY(1,1) NOT NULL, EmpName VARCHAR(100) NOT NULL, EmpSal FLOAT NOT NULL, DateOfJoining DATETIME NOT NULL DEFAULT(GETDATE()) ) CREATE TABLE EmployeeLog ( EmpId NUMERIC(18, 0) IDENTITY(1,1) NOT NULL, EmpName VARCHAR(100) NOT NULL, EmpSal FLOAT NOT NULL, DateOfJoining DATETIME NOT NULL DEFAULT(GETDATE()) ) I have an insert trigger defined on the table Employee which inserts a new record in the EmployeeLog whenever a record insert in the Employee table. So Suppose I insert a new record in the Employee table using following statement: INSERT INTO Employee (EmpName,EmpSal) VALUES ('Arefin','1') The trigger will be fired automatically and insert a record in EmployeeLog. Here the scope of the insert statement and the trigger are different. In this situation if I retrieve last inserted identity value using @@IDENTITY, it will simply return the identity value from the EmployeeLog because it’s not limited to a particular scope. Now if I want to get the Employee table’s identity value then I need to use SCOPE_IDENTITY in this scenario. So the moral is always use SCOPE_IDENTITY to return the identity value of a recently created record in a sql statement or stored procedure. It’s safe and ensures bug free code.

    Read the article

  • Oracle global lock across process

    - by Jimm
    I would like to synchronize access to a particular insert. Hence, if multiple applications execute this "one" insert, the inserts should happen one at a time. The reason behind synchronization is that there should only be ONE instance of this entity. If multiple applications try to insert the same entity,only one should succeed and others should fail. One option considered was to create a composite unique key, that would uniquely identify the entity and rely on unique constraint. For some reasons, the dba department rejected this idea. Other option that came to my mind was to create a stored proc for the insert and if the stored proc can obtain a global lock, then multiple applications invoking the same stored proc, though in their seperate database sessions, it is expected that the stored proc can obtain a global lock and hence serialize the inserts. My question is it possible to for a stored proc in oracle version 10/11, to obtain such a lock and any pointers to documentation would be helpful.

    Read the article

  • How to see when stored procedures have last run

    - by Brandon Moore
    I want to see a listing of all the stored procs for each database on a server along with when the last time that store proc was run. I'm pretty good with SQL but I don't know about looking at stats like this that sql keeps so I'd appreciate a little help finding this info. EDIT: From the answers I'm getting it sounds like this is not possible the way I thought it would be. I was thinking that it could be done similarly to how you can see when a table was last accessed: select t.name, user_seeks, user_scans, user_lookups, user_updates, last_user_seek, last_user_scan, last_user_lookup, last_user_update from sys.dm_db_index_usage_stats i JOIN sys.tables t ON (t.object_id = i.object_id) where database_id = db_id() The above script was stolen from a comment on http://blog.sqlauthority.com/2009/05/09/sql-server-find-last-date-time-updated-for-any-table/.

    Read the article

  • nested insert exec work around

    - by stackoverflowuser
    i have 2 stored procedures usp_SP1 and usp_SP2. Both of them make use of insert into #tt exec sp_somesp. I wanted to created a 3rd stored procedure which will decide which stored proc to call. something like create proc usp_Decision ( @value int ) as begin if (@value = 1) exec usp_SP1 -- this proc already has insert into #tt exec usp_somestoredproc else exec usp_SP2 -- this proc too has insert into #tt exec usp_somestoredproc end Later realized I needed some structure defined for the return value from usp_Decision so that i can populate the SSRS dataset field. So here is what i tried: within usp_Decision created a temp table and tried to do "insert into #tt exec usp_SP1". Didn't work out. error "insert exec cannot be nested" within usp_Decision tried passing table variable to each of the stored proc and update the table within the stored procs and do "select * from ". That didnt work out as well. Table variable passed as parameter cannot be modified within the stored proc. Pls. suggest what can de done?

    Read the article

  • Performance optimization for SQL Server: decrease stored procedures execution time or unload the ser

    - by tim
    We have a web service which provides search over hotels. There is a problem with performance: a single request to the service takes around 5000 ms. Almost all of the time is spent in database by executing storing procedures. During the request our server (mssql2008) consumes ~90% of the processor time. When 2 requests are made in parallel the average time grows and is around 7000 ms. When number of request is increasing, the average time of response is increasing as well. We have 20-30 requests per minute. Which kind of optimization is the best in this case having in mind that the goal is to provide stable response time for the service: 1) Try to decrease the stored procedures execution time 2) Try to find the way how to unload the server It is interesting to hear from people who deal with booking sites.

    Read the article

  • SQL Server missing tables and stored procedures

    - by Robo
    I have an application on a client's site that processes data each night, last night SQL Server 2005 gave the error "Could not find stored procedure 'xxxx'". The stored procedure does exist in the database, has the right permission as far as I can tell, the application runs fine in other nights as well. In previous occasions, the SQL Server has also gave error saying 'database object not found', and refers to a table in the database that does exists. So, on rare occasions, the server thinks certain stored procedures and tables does not exist in the database. The objects it refers to are often ones that are frequently used. Is the database somehow corrupted, is there some sort of repair/health check I can do?

    Read the article

  • Is there any tool which can show the call tree for SQL stored procedures

    - by DBZ_A
    I have a huge SQL script which i need to analyse. It would be really helpful if i could find a tool which can generate a call tree; ie, to see which all procedures are called from a particular procedure. a perl based example is here, http://sqlblog.com/blogs/linchi_shea/archive/2009/10/23/find-the-complete-call-tree-for-a-stored-procedure.aspx but i need a tool to analyse the text file (.sql file), not the procedure stored in the database. due to some reasons i will not be able to create the whole set of procedures in the database and use the above mentioned tool. please respond if you have come across any ide/tool with this feature.

    Read the article

  • Calling SQL Azure's stored procedure in silverlight

    - by sweetsweet
    I have a silverlight application to call the SQL Azure's stored procedure and show the data. I use the service to call stored procedure, and it is working when it is run at VS 2008. But when I integrate in php page, the data is not shown. I think the service is not running. How can I call SQL Azure stored procedure without using service? I cannot use ADO.Net Entity Domain Model and LINQ to SQL(I don't have permission to access that database in GUI). I cannot connect to SQL Azure, so which method can I use to call SQL Azure stored procedure in silverlight? Please give me the information. {I don't know very much about that silverlight. To take the detailes of my project. I want to call SQL Azure's stored procedure. So, I tried to call that Stored Procedure from ADO.Net Entity Domain Model. The problem is I cannot access that SQL Azure database. It show "Buit_in function suer_sname is not supported in this version of SQL server. Therefore, I connect to that Stored Procedure in code beind and call that function in Service and to use that data in presentation layer, I add service reference in silverlight(I mean the service is at the same project). It is perfectly working when it is running at VS. So,I take out xap file and call that xap file in php. The silverlight is running in that PHP page but the data is not shown. I think the service is not hosted. So, how can I solve my problem? I use xampp to run my php page. }

    Read the article

  • PostgreSQL triggers and passing parameters

    - by iandouglas
    This is a multi-part question. I have a table similar to this: CREATE TABLE sales_data ( Company character(50), Contract character(50), top_revenue_sum integer, top_revenue_sales integer, last_sale timestamp) ; I'd like to create a trigger for new inserts into this table, something like this: CREATE OR REPLACE FUNCTION add_contract() RETURNS VOID DECLARE myCompany character(50), myContract character(50), BEGIN myCompany = TG_ARGV[0]; myContract = TG_ARGV[1]; IF (TG_OP = 'INSERT') THEN EXECUTE 'CREATE TABLE salesdata_' || $myCompany || '_' || $myContract || ' ( sale_amount integer, updated TIMESTAMP not null, some_data varchar(32), country varchar(2) ) ;' EXECUTE 'CREATE TRIGGER update_sales_data BEFORE INSERT OR DELETE ON salesdata_' || $myCompany || '_' || $myContract || ' FOR EACH ROW EXECUTE update_sales_data( ' || $myCompany || ',' || $myContract || ', revenue);' ; END IF; END; $add_contract$ LANGUAGE plpgsql; CREATE TRIGGER add_contract AFTER INSERT ON sales_data FOR EACH ROW EXECUTE add_contract() ; Basically, every time I insert a new row into sales_data, I want to generate a new table where the name of the table will be defined as something like "salesdata_Company_Contract" So my first question is how can I pass the Company and Contract data to the trigger so it can be passed to the add_contract() stored procedure? From my stored procedure, you'll see that I also want to update the original sales_data table whenever new data is inserted into the salesdata_Company_Contract table. This trigger will do something like this: CREATE OR REPLACE FUNCTION update_sales_data() RETURNS trigger as $update_sales_data$ DECLARE myCompany character(50) NOT NULL, myContract character(50) NOT NULL, myRevenue integer NOT NULL BEGIN myCompany = TG_ARGV[0] ; myContract = TG_ARGV[1] ; myRevenue = TG_ARGV[2] ; IF (TG_OP = 'INSERT') THEN UPDATE sales_data SET top_revenue_sales = top_revenue_sales + 1, top_revenue_sum = top_revenue_sum + $myRevenue, updated = now() WHERE Company = $myCompany AND Contract = $myContract ; ELSIF (TG_OP = 'DELETE') THEN UPDATE sales_data SET top_revenue_sales = top_revenue_sales - 1, top_revenue_sum = top_revenue_sum - $myRevenue, updated = now() WHERE Company = $myCompany AND Contract = $myContract ; END IF; END; $update_sales_data$ LANGUAGE plpgsql; This will, of course, require that I pass several parameters around within these stored procedures and triggers, and I'm not sure (a) if this is even possible, or (b) practical, or (c) best practice and we should just put this logic into our other software instead of asking the database to do this work for us. To keep our table sizes down, as we'll have hundreds of thousands of transactions per day, we've decided to partition our data using the Company and Contract strings as part of the table names themselves so they're all very small in size; file IO for us is faster and we felt we'd get better performance. Thanks for any thoughts or direction. My thinking, now that I've written all of this out, is that maybe we need to write stored procedures where we pass our insert data as parameters, and call that from our other software, and have the stored procedure do the insert into "sales_data" then create the other table. Then, have a second stored procedure to insert new data into the salesdata_Company_Contract tables, where the table name is passed to the stored proc as a parameter, and again have that stored proc do the insert, then update the main sales_data table afterward. What approach would you take?

    Read the article

  • Linq2SQL using Update StoredProcedure

    - by PeterTheNiceGuy
    We use queries generated by Linq for data retrieval but for INSERT and UPDATE we do not allow generated SQL, but restrict to the use of stored procedures. I connected the Update and the Insert behaviour in the DBML to the stored procedures. The procedures are called, the data gets inserted/updated = all if fine, except in the case of optimistic concurrency. If a record was changed between retrieval and update, the update should fail. When Linq generates the Update statement itself, it throws a ChangeConflictException as expected, but using the stored procedure no Exception is thrown. Thanks a lot for any help on this!

    Read the article

  • List stored functions using a table in PostgreSQL

    - by Paolo B.
    Just a quick and simple question: in PostgreSQL, how do you list the names of all stored functions/stored procedures using a table using just a SELECT statement, if possible? If a simple SELECT is insufficient, I can make do with a stored function. My question, I think, is somewhat similar to this other question, but this other question is for SQL Server 2005: http://stackoverflow.com/questions/119679/list-of-stored-procedure-from-table (optional) For that matter, how do you also list the triggers and constraints that use the same table in the same manner?

    Read the article

  • Finding all Stored procedures calling a function

    - by Abhishek Jain
    Hi, How can I find out all the stored procedures that are calling a particular user defined function in SQL Server 2005. Or how to assign a defult value to a parameter in a user defined function so that when a stored procedure calls that function and does not pass any value to that parameter function assumes the default value. Regards, Abhishek jain

    Read the article

  • Stored Procedures last access time?

    - by Ardman
    Is it possible to find out when a Stored Procedure was last accessed? I tried the following: SELECT * FROM sys.dm_db_index_usage_stats WHERE [database_id] = DB_ID() AND [object_id] = OBJECT_ID('stored procedure name') and it returns a blank resultset.

    Read the article

  • Where are global variables stored in memory?

    - by Jack
    Are they stored in the heap or there is separate area where they are stored? Similarly where are static variables stored in memory. They can't be stored in frames as they will get destroyed when the function returns. PS - Can someone suggest a good book that talks about memory mapping for C/C++.

    Read the article

  • Settings for multiple monitors are not stored

    - by JJD
    I am running Ubuntu 12.04. on a Lenovo Thinkpad T400. I connected an external monitor as a second display. The laptop stands under the external screen. The laptop has a native resolution of 1440x900 (16:10), the external monitor 1280x1024 (5:4). There are two graphic adapters: one internal Intel GMA 4500 MHD and an discrete ATI card. Currently, the integrated Intel is enabled. I use the Display application to arrange the position of the monitors so it look like this: The problem: Whenever I restart my computer the configuration gets lost. First, the displays are mirrored instead of extended. I have to press Fn + F7 two times to switch to extended mode. Second, the Display settings still look like this: I know this worked once when I was running Ubuntu 10.10. I cannot tell since when it does not work. Do you know how I can permanently store the settings?

    Read the article

  • Access local email stored on worstation on laptop on lan

    - by crafter
    I have the following scenario with my email : I am using Evolution as my primary email client on my workstation. The evolution mail is downloaded from my mail servers using POP, then deleted from the server. When I am mobile, I access my email on my email server using webmail. My laptop is my primary computer thesedays. The workstation is hardly used. When I am mobile, I am restricted to new email that has not been downloaded onto the workstation I am now looking for a way to access my email from my workstation on my laptop, amlost as if my workstation is my second level email server/ I tried evolution on X display but attachments will browse on my workstation (not ideal as most docs are on my laptop). I am open to changing mail client or installing a service on my workstation. What would be the best way to address this requirement?

    Read the article

< Previous Page | 17 18 19 20 21 22 23 24 25 26 27 28  | Next Page >