Do Cursors Lose Their Place If Deletes And Multiple Commits Are Occuring On The Underlying Table?
Oct 11, 2007
will my fetches on a cursor continue seamlessly even if deletes and multiple commits are occuring on the underlying source table? I want to make sure the cursor will not lose it's place as I believe some older dbms's did.
I want to use one stored procedure to delete rows in three different tables - how do I write this for Sql Server 2000 please? PROCEDURE dbo.DeleteSubmission @C_ID intASDELETE *FROM tblCompaniesWHERE C_ID = @C_ID DELETE *FROM tblStoresWHERE CS_ID = @C_ID DELETE *FROM tblDistributorsWHERE CD_ID = @C_ID RETURN
We have a Master table in the Db who's PK is referenced in at least 60 tables as FK. We want to delete all the dependent records in 1 go without using multiple delete statements or any cursor or loop.
Just wondering if this is good form:Alter Procedure "mySPName"@UniqueID intASset nocount onset xact_abort offDELETE FROM tblNameOneWHERE(tblNameOne.UniqueID = @UniqueID)DELETE FROM tblNameTwoWHERE(tblNameTwo.UniqueID = @UniqueID)Is it a good idea to run multiple detele statements within one SP?thanks,lq
I am building a customerlist within the customer sales of a period. I have a dataset with two tables: "customer" and "customer_ledger_entry".
In the report I will present the customer number, the customer name and the sales. The problem is the sales value is not available as a field, but I have to calculate this value from the "customer_ledger_entry" table. In this table are several entries (invoices, credit notes, etc.)
How to calculate the values from a underlying table?
I want to grant access on the below view for an end user so that he connect to our SQL server and retrieve data. The view looks like the below
CREATE VIEW DB1.[dbo].[View1] AS -- For brevity, I made it as simple statement. SELECT * From DB2.dbo.table2 GO
For the above view, it looks like I have to grant select and connect permission for the DB1. [dbo].[View1] as well as DB2.dbo.table2.
1. Is my understanding correct?
2. I want the user to access only DB1. [dbo].[View1] and not the underlying tables. Is there a way to grant access only on the view and execute the statement on a different security context so that the user can€™t access DB2.dbo.table2 directly?
3. When the user uses SQL Server Management Studio to connect to SQL server, he is able to connect and select DB2.dbo.table2 directly. Is there any way to restrict user from viewing and executing select statement on DB2 database from SQL Server Management Studio
I need to create a table that only allows records to be inserted or deleted. Once the record has been created it can only be deleted. Is there anyway to configure a table in this manner?
Table Definition
USE [DB_AUTOMATED_PACKAGING_SYSTEM] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[TBL_PCL_LENS_DATA]( [SerialNumber] [varchar](50) NOT NULL, [ProcessedDate] [datetime] NOT NULL, [Filename] [varchar](50) NOT NULL, [CartonLabelImage] [image] NOT NULL, [ExpirationDateLabelImage] [image] NOT NULL, [LabelSetLabelImage] [image] NOT NULL, [ReplyCardLabelImage] [image] NOT NULL, [TextFile] [ntext] NOT NULL, CONSTRAINT [PK_TBL_PCL_LENS_DATA] PRIMARY KEY CLUSTERED ( [SerialNumber] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
I am coding under Delphi a software which will be using a SQL Server 2000. I am using ADO components and have the latest MDAC: 2.8 SP1.
The software is using a lot of TADOQuery objects which are connected to the same TADOConnection. The TADOQuery having for options : - cursorlocation : useclient - cursortype : static - locktype : optimistic
On the first use of a TADOQuery in order to execute any type of request, the processing is fast. If I am using this same object for others requests, everything is fine and as fast as the first request. But as soon as I am using another TADOQuery, performances are going down.
In order to give some numbers, some requests can take as much as 17 seconds to be executed. The same request would take at most 100 milliseconds if it were executed with the first TADOQuery.
It is not a problem concerning indexes; if I try to use the request analyzer, all my requests are processed quickly.
I used the SQL Profiler to see the details and saw some interesting things : the first uses of TADOQuery are of type SQL:BatchCompleted but switching to another TADOquery make it use another type : RPC:Completed. Contents of this RPC can be a declaration or an execution of cursor.
More precisely, if I am using only one ADOQuery, I see one line in the Profiler for every block of code opening/reading contents/closing. But using multiples ADOQuery, I see one declaration of cursor for the opening and then every reading produces one cursorfetch. So, this declaration of cursor may take a long of time and every fetch too and if there is many lines to send to the client, the network is used for each line to send.
Better, using an TADOQuery to make a request and then simply initializing another one by changing the SQL property and then executing a second time the first TADOQuery makes this last execution used as a cursor serverside. The problem should then on the side of the client but I can not figure where.
I hope having been as clear as needed ! My wishes is to not have recode the whole database side of our software because it is huge and badly designed so it will be hard to test everything fine. So before recoding I wish to continue the analysis.
So does anyone knows why is happening this serverside thing ? And is there a simple way to make it not happen ?
I am trying to create a stored procedure that Deletes Table if there is Data.
Also stored procedure will Insert new data into table.
I have already created table. This is part of my current stored procedure.
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'srd.[PNMACjmerlos].FHLMC_Trials') AND type in (N'U')) DELETE srd.[PNMACjmerlos].FHLMC_Trials
I have two databases DB1 and DB2 DB1 has a source table named 'Source' I have created a login 'Test_user' in DB2 with Public access. I have also created a view named 'Test_view' in DB2 which references data from DB1.dbo.Source
I have two databases DB1 and DB2 DB1 has a source table named 'Source' I have created a login 'Test_user' in DB2 with Public access. I have also created a view named 'Test_view' in DB2 which references data from DB1.dbo.Source
We've experienced a problem with one of our MSSQL7 servers where transactions are not being committed to the database. We've experienced this in Query Analyzer -- where we run several (simple) insert or update statements. Although the script runs successfully, the rows do not appear in the tables. The implicit transaction setting is off for the server. We have also experienced this problem with other tools that run against this server via ODBC. I thought the default setting for MSSQL7 is to auto-commit transactions and I've seen nothing in the error log that indicates that the updates were rolled back. I have compared our server, database and client configuration options to other servers that do not have this problem and have no idea where to look next. Has anyone encountered this problem? Or does anyone have some ideas about where I could look for more information? Thanks!
I have a Insert,Update trigger on a table called TBL_TEST . I am doing inserts in 4 tables inside the trigger. But if any one of these insert fails I want to keep the changes made in any previous table but rollback the insert / update made on TBL_TEST.
When I encounter an error and issues rollback its rolling back all the inserts. Even though I am using exclusive begin tran - Commit Tran for every insert statement.
Is there a TSQL function or value that is assigned to some sort of internal variable when part of a distributed transcation commits? If so, how do I access it? Marc
I suspect I have a but in a stored procedure that is used to delete records in a table. Here's the code for the gridview and its SQLDataSource 1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 2 DataKeyNames="table2_id" DataSourceID="SqlDataSource1"> 3 <Columns> 4 <asp:CommandField ShowDeleteButton="True" ShowSelectButton="True" /> 5 <asp:BoundField DataField="table2_id" HeaderText="table2_id" 6 InsertVisible="False" ReadOnly="True" SortExpression="table2_id" /> 7 <asp:BoundField DataField="table1_id" HeaderText="table1_id" 8 SortExpression="table1_id" /> 9 <asp:BoundField DataField="name_2" HeaderText="name_2" 10 SortExpression="name_2" /> 11 <asp:CheckBoxField DataField="yesno" HeaderText="yesno" 12 SortExpression="yesno" /> 13 <asp:BoundField DataField="MyArtificialColumn" HeaderText="MyArtificialColumn" 14 ReadOnly="True" SortExpression="MyArtificialColumn" /> 15 </Columns> 16 </asp:GridView> 17 <asp:SqlDataSource ID="SqlDataSource1" runat="server" 18 ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString %>" 19 DeleteCommand="EraseIt" DeleteCommandType="StoredProcedure" 20 SelectCommand="StoredProcedure5" SelectCommandType="StoredProcedure"> 21 </asp:SqlDataSource> Recently, "Wreck of u" kindly provided me with the stored procedure "StoredProcedure5" (line 20) which populates the gridview as shown. What I would like to now is write a stored procedure that DELETES a row. As like 19 shows, I attempted to write such an SP called EraseIt. However, when I click on Delete in the browser for a row, I get the error message "Procedure EraseIt has no parameters and arguments were supplied. The following is the code for EraseIt. Do I in fact have an error with specifying the input parameter "@table2_id", and or am I doing something else wrong? 1 ALTER PROCEDURE [dbo].[EraseIt] 2 AS 3 DECLARE @TABLE2_ID int 4 BEGIN 5 DELETE FROM DBO.TABLE2 WHERE TABLE2_ID = @TABLE2_ID 6 END 7 Thanks!-Kurt
Let's say you have a Users table, which simply contains a collection of people. Each person has attributes like their password, first and last name, username, etc. It would be useful to track changes to that table over time, so that if an entry is changed, you can simply look at a history table and see which changes occured, and when.
An INSERT/UPDATE trigger works well for this sort of thing, where the trigger inserts all of the 'INSERTED' values into a history table that has basically the same table definition as the Users table. But it gets tricky when considering Deletes.
If my History table entries reference back to the User in the Users table, this means that if I ever want to delete the user, I need to delete all their History first. This means I can't keep records of user deletions, which is significant. The other approach is not to have a foreign key reference in the History table, so that if a user is deleted, I can still keep my History about that user. Including deletes.
I have been timid about doing it this way, since I felt it broke the idea of a well structured database. Is there a better way to approach this? Is there a standard way to set up triggered history to track changes, including deletions, from a table?
Hello. Have a strange one here. I'm backing up databases locally to a driveon the server daily. I needed to change the time of the backup, and didthat, but it's still occuring at the old time.I deleted the old job, and recreated it with the new time. I won't knowuntil tonight at 12:00 whether that worked or not.Can someone tell me where it saves the maintenance schedules and how itkicks it off? I'd like to take a look at it to see if it's still there.I'm a little bit of a newbie on SQL server, so be gentle. :-)--If responding to me directly, please take out "REMOVE" from my e-mailaddress.Thanks!Doug
I've dropped a table control in the body of a report. I've moved this table 10 cm down from the top of the report. When I'm browse the report is looks ok , the page 1,2... But when I print the report, the first page is ok ( the table begin 10 cm down the top), but in the 2 and 3 ... pages the table begin to print with 0 cm from the top.
After having built a decision tree model to predict a boolean output attribute using 64-bit SQL Server 2005 (build 9.0.3054), we have observed that predictions for some cases are being done at non-leaf nodes in the tree.
Specifically, after executing a prediction join which returns:
and comparing the values of PredictNodeID(MiningModel.OutputAttribute) with the mining model content column [NODE_UNIQUE_NAME] to determine the actual "rule" used to make the case-level prediction.
We have observed that for a subset of cases, predictions are being made at nodes in the tree that are not leaf nodes. Specifically, predictions are being made at a node that is 3 levels deep. The leaf nodes below this inner-tree node are 2 levels further down the tree.
Also supporting the fact that that predictions are being made at this non-leaf node is that the PredictProbability corresponds exactly with the output attribute distribution at this non-leaf node.
In this particular application, we would have obtained better results if the predictions were made at the leaf-nodes.
A few questions: 1. Why are predictions with decision trees made at non-leaf nodes? 2. Is there a way to "force" predictions to occur at leaf nodes via DMX?
I recently updated the datatype of a sproc parameter from bit to tinyint. When I executed the sproc with the updated parameters the sproc appeared to succeed and returned "1 row(s) affected" in the console. However, the update triggered by the sproc did not actually work.
The table column was a bit which only allows 0 or 1 and the sproc was passing a value of 2 so the table was rejecting this value. However, the sproc did not return an error and appeared to return success. So is there a way to configure the database or sproc to return an error message when this type of error occurs?
I am starting to create a new database table based on an existing dbtable. My existing table has a list of ID's and a date range for each ID.
For example: TableSource ID Start DateTime End datetime Y10012 01-12-12 13:00:00 01-19-12 13:00:00
So for this ID, I need my SQL statement to read this table, then create a new table and insert a new row for every second starting from the start date to the end date. I have several id's that span a week at a time. So I am expecting millions and millions of records once I am done.
Then once it completes reading S1001, it moves on to the next ID and appends it to the table. The date ranges are different for each ID- so it can't be hard coded.
I've been looking online and cannot find any help / resources with this so I brought it here :D I'm looking for help in creating a Cursor (this will be inside a SP) that will loop through the records of a "Table" (Temporary or Retrieved) and for each row that is looped through I can use it's values to do inserts against a few other tables. Any resources / help would be great! I work best by example.
Hello,Using SQL Server 2000, I'm trying to put together a query that willtell me the following information about a view:The View NameThe names of the View's columnsThe names of the source tables used in the viewThe names of the columns that are used from the source tablesBorrowing code from the VIEW_COLUMN_USAGE view, I've got the codebelow, which gives me the View Name, Source Table Name, and SourceColumn Name. And I can easily enough get the View columns from thesyscolumns table. The problem is that I haven't figured out how tolink a source column name to a view column name. Any help would beappreciated.Garyselectv_obj.name as ViewName,t_obj.name as SourceTable,t_col.name as SourceColumnfromsysobjects t_obj,sysobjects v_obj,sysdepends dep,syscolumns t_colwherev_obj.xtype = 'V'and dep.id = v_obj.idand dep.depid = t_obj.idand t_obj.id = t_col.idand dep.depnumber = t_col.colidorder byv_obj.name,t_obj.name,t_col.name
Hello,I have one application which is having written in asp.net & plain asp.I am having one button on asp page,when i will click on that button, then itwill execute one other asp page.And after the execution of that second asp page, I redirect it to someASPX page with some values.On the ASPX page, it will connect to the Database, and insert the values.Thus, sometime, the following error is occuring :"Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding."Main thing is that i am testing it on my local machine, then also it is giving me this error ...Insert into statement is also not much complicated :just I inserted three values in one table.Is this occuring due to the mixer of asp & aspx??Plz give me some proper solution.I want it in efficient manner, because this application will be accessed by number of users at the same time.Plz help me.Thanks,Sandy
I have been wrestling with the code all day to accomplish the following: I need to update a table based on values from another table. So far, I have been able to do the below:
DECLARE @LookUpTerm VARCHAR(25) , @SearchCol VARCHAR(255) , @LogonIDToProcess VARCHAR(50) , @Matched CHAR , @Cycle INT = 1 IF OBJECT_ID('tempdb..#Glossary','U') IS NOT NULL DROP TABLE #Glossary IF OBJECT_ID('tempdb..#Employees','U') IS NOT NULL DROP TABLE #Employees
Two tables t1 and t2 have the same schema but exist on two different servers. Which is the better technique for updating t2 and why?
/****** Object: Table [dbo].[t1] Script Date: 9/6/2007 9:55:21 AM ******/ if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[t1] GO
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) BEGIN CREATE TABLE [dbo].[t1] ( k [int] IDENTITY (1, 1) NOT NULL , a [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , b [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , c [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , x [int] NULL , y [int] NULL , amt [money] NULL ) ON [PRIMARY] END
GO
/****** Object: Table [dbo].[t2] Script Date: 9/6/2007 9:55:44 AM ******/ if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[t2] GO
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t2]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) BEGIN CREATE TABLE [dbo].[t2] ( k [int] IDENTITY (1, 1) NOT NULL , a [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , b [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , c [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , x [int] NULL , y [int] NULL , amt [money] NULL ) ON [PRIMARY] END
GO
-- Technique 1:
set identity_insert t2 on
insert into t2 (k,a,b,c,x,y,amt) select k,a,b,c,x,y,amt from t1 where not exists (select k from t2 where t1.k = t2.k)
set identity_insert t2 off
update t2 set a = t1.a, b = t1.b, c = t1.c, x = t1.x, y = t1.y, amt = t1.amt from t1 where t1.k = t2.k
-- Technique 2: set identity_insert t2 on
declare t1_cur cursor for select k,a,b,c,x,y,amt from t1 for read only
open t1_cur
declare @k int declare @a char(10) declare @b char(10) declare @c char(10) declare @x int declare @y int declare @amt money
fetch next from t1_cur into @k,@a,@b,@c,@x,@y,@amt while(@@FETCH_STATUS = 0) begin if exists(select k from t2 where k = @k) begin update t2 set a = @a, b = @b, c = @c, x = @x, y = @y, amt = @amt where (k = @k) end else begin insert into t2 (k,a,b,c,x,y,amt) values(@k,@a,@b,@c,@x,@y,@amt) end
fetch next from t1_cur into @k,@a,@b,@c,@x,@y,@amt end
close t1_cur deallocate t1_cur
set identity_insert t2 off
Thanks,
Joel K Database Adminstration/Application Development
I'm using SQL Server 2005 as my backend application. I had strange experience on it, sometime some of my records suddenly disappear without any deletion action.I've check the log files and I've already set a trigger on deletion to copy the deleted record to a history table, but I found nothing. Are there anybody that have same experience like me ?
We are running SQL 7 with a front end that links to the tables through ODBC.In our main table, the user has no way to delete a record through theinterface, though it is possible to delete it by opening the ODBC link.Users would have no reason to delete a record, but one of our records turnedup missing.Now, it's possible that a user may have accidentally deleted the record.But, since users don't have any reason to delete records, and since theydon't access the ODBC links, it seems unlikely (though possible).I was wondering if anyone had every heard of SQL Server ever "losing" arecord that had previously been saved. I checked the nightly backup from thenight after it was added, and the record was there. So either a user deletedit, or somehow it got lost in SQL Server. I have no code that deletesrecords in this table in any way, shape or form, so it couldn't have beenmalfunctioning code.So, while I have a hard time believing that SQL Server would just "lose" arecord, I also know that anything's possible, so I thought I'd ask if anyonehad ever heard of such a thing.Thanks!Neil