hi guys i was wondering if anyone could help me, i have a table with a field called id that did have numbers 1,2,3,4,5,6,7,8 and so on! but after some tinkering i have removed a few value and added more so i now have 1, 4,8,19,20 and so on!
i was wondering if i can run a query to update those value and return them to 1,2,3,4,5,6,7,8 and so on?
Cheers
Tupps
What is the easiest way in T-SQL to reset the identity values of the PK columns in a DB. Here's the scenario.
I'm going to be backing up a HUGE DB and then restoring it to a new server. Once there I will run an SP to remove all the existing data. The tables are now row-free. However my customer doesn't like that the first record they add gets PK value of 5,321, or whatever.
After I remove all the data how can I reset the ids? It would be nice to add some code to my sp_cleardb that would go back and reset the identity columns FYI All of my tables use auto-incrementing identity fields with a seed of 1
1) I know how to do it in enterprise manager, but I need it in scripting since I'll have to do it on hundreds of tables 2) I tried using alter table to drop the PK constraint, but that requires knowing the pk constraint name, which is SQL generated and I won't know.
I think I got very close, but the code was just getting too big. I thought, there must be an easier function to use, or a built in SP, or something?
Anyone know how to script this? THanks in advance. Josh
Hi,I have a table OutMailDetails with the following fields : OutMailID --- Foriegn Key Name Description My problem is that i have datas already in this table and the table is related to another table called OutMail through OutMailID field. Please how can i make it (OutMailID field) a primary key now.
i am using autonumber as a primary key in one table. in my app., row from that table will be moved to another table after some time but that autonumber is important. when there are no any data in first table, and new row is inserted, autonumber starts from 1 which i don't want as there will be data redundancy when it will be moved to another table. so is there any way i can force the autonumber to start from previous value rathe than 1?
hi!i'm new to sql server and enterprise manager and i accidentally deleted all data in a table. how do i recover them? the table (CaptureManager) is still there but empty. i have the following information:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[CaptureManager]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[CaptureManager]GO CREATE TABLE [dbo].[CaptureManager] ( [ID] [tinyint] IDENTITY (1, 1) NOT NULL , [Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Email] [varchar] (75) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Ext] [varchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Status] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO
Hey! I downloaded the sql engine and made a bunch of tables in visual basic. They were called new1, new2, new3, and new4. So I deleted new1.mdf, new1.ldf, new2.mdf, etc... but I notice I still can't recreate them. So I'm thinking there's remnants in master.mdf or something?? How would I go about getting rid of all the old tables, even ones whose names I've forgotten, so I can have a clean slate again? (btw I don't have sql server, just the desktop engine)
I am trying to reset an IDENTITY RowNumber back to 1 in a query when the Person's Name is the same. The following query can be used agains the pubs database in SQL Server.
I am trying to figure out where to place the section for the DBCC CHECKIDENT item in order to reset the RowNumber back to 1 when ever the lname column is the same. For example if the pub database had duplicate records for a given employee the RowNumber would reset back to 1.
CREATE TABLE #RowNumber ( RowNumber int IDENTITY (1, 1), emp_id char(9) ) --DECLARE @seed int --SET @seed = 1 --DBCC CHECKIDENT ('#RowNumber', RESEED , @seed) WITH NO_INFOMSGS
INSERT #RowNumber (emp_id) SELECT emp_id FROM employee ORDER BY lname SELECT RowNumber, e.emp_id, lname, fname, job_id FROM #RowNumber r JOIN employee e ON r.emp_id = e.emp_id
Hello all- Is it possible to reset the values of DMV stats/counters without restarting the SQL service? I'm looking for something more than dbcc freeproccache...more along the lines of index_usage and some of the OS DMVs.
This is the best suited forum I found for my problem, if there's a better suited one please direct me.
I am running SQL Server 7.0 on Windows 2000 Server, and the SA's password has been forgotten and no other employee knows it. I searched the net for a solution, and I found various pages describing how to reset the SA's password by logging in an administrator, connecting to the server and using a sp_password command.
For some reason, I cannot connect to the server even when I'm logged on as administrator. Here are the steps I took:
1) I logged in as DOMAINAdministrator at my domain controller. 2) I opened Enterprise Manager and clicked on the server labeled "(LOCAL)". The server is registered to use "Windows NT authentication". 3) Received the following error:"A connection could not be established to (LOCAL) - Login failed for user 'DOMAINAdministrator'.. Please verify that SQL Server is running and check your SQL Server registration properties ... and try again". If there was an option to attach a screenshot, I would, but... 4) I have verified that SQL Server is running.
As I am new to SQL Server, I'm not sure about this, but the SQL Server is called SERVERNET, and the domain I'm logged into is called DOMAIN. Should I login as an administrator to SERVERNET and not to DOMAIN? Is that possible? "SERVERNET" does not show on the login screen, only "DOMAIN" is listed there.
Question - if you had to completely strip all permissions from all databases in an instance and reset them, assuming you have metadata to support rebuilding the permissions, what steps would you follow? I can handle the iterating through each database, but at the database level, what steps would you take?
The reason I inquire on this is I currently have a job that I inherited that does just this. But it's buggy and was also written in for SQL Server 2000. With some of the changes in 2005, a few bugs have crept in, etc. And I would like to confirm my thoughts this. Or, if your opinion is why are you wasting your time on this? Then that's fine to and I'll review any constructive comments you may have.
I've run into an issue that's been driving me up a wall.
Running SQL Server 2005, two instances production and development, report server on each. I have a multiple scheduled reports on each instance, and I've over wrote the report server subscription with a SQLAgent schedule. For whatever reason the reports on the production instance keep having their schedule reset to the subscription when the report was first created. The logs give me a warning that the schedule is changing or deleted but it's happening when no one is around.
Can anyone shed some light on this for me? or am I missing something really obvious here?
I used a lot of fictitious data in order to test the code of my web site. But I noticed that when I deleted all fictitious data from the tables, the primary key columns keep creating automatically numbers that are above the last one before all data was erased instead of starting again from number 1. How do I reset this automatic numeration in order to restart from 1 again ?
Can anyone tell me of a way to reset the value of an Identity field back to its seed value after all the rows in a given table have been deleted and I try to re-populate it? There are foreign key relationships so I cannot just drop and recreate the table, and I really need to address this issue from within sql, rather than from the application.
I used a lot of fictitious data in order to test the code of my web site. But I noticed that when I deleted all fictitious data from the tables, the primary key columns keep creating automatically numbers that are above the last one before all data was erased instead of starting again from number 1. How do I reset this automatic numeration in order to restart from 1 again ?
I have installed the reports for the performance dashboard and really like it!
However, I'd like to be able to clear the stats in order to run specific procedures etc and see the most inefficient parts in the specific procedure. The documentation says like this about it:
The lifetime of the DMV data depends on the lifetime of the plan in cache. You can determine how long the plan has been cached, and thus the time frame over which these resources have been consumed, by looking at the Plan Cached column
What Plan Cashed column? Can someone explain to me how to clear the stats?
Hi I have a DataSource control which is currently set with no initial SelectCommand Property when it is constructed. I have a number of standard buttons, each of which when clicked fires an event that analyses and modifies the SelectCommand for the DataSource. Each event sets the SelectCommand Property for the DataSource using eg:dataSource.SelectCommand = "SELECT * FROM table WHERE tableID IN ('1', '2', '3')";(To help you understand, the aim is to have each button either inserting or removing an ID number into an SQL 'IN' clause as shown above) When the events are fired the DataSource is updated with the new SelectCommand and the bound ListBox updates correctly. However any subsequent events don't seem to read the previously modified SelectCommand setting. When they try to read the SelectCommand using:string queryToModify = dataSource.SelectCommand;They just get a empty string returned instead of the query that the previous button click set (temporarily). It seems like a state issue, but as a server side control it should automatically keep the state, right? Thanks for taking the time to read this and many thanks in advance for any assistance (I've only been learning ASP.NET for a few days... and I'm quite impressed so far :-) ) PurplePerson
Any additional help besides checking named pipes for the connectivity 10004 problem on 6.5. Did a client software upgrade and now cannot access the database. Also, how can I reset the sa password?
I have question regarding updating user password in sql 2005.
Is there a way to update user password in tsql without specify the old password?
In sql 2000, since we are still able to mess with the data in system table, I can use update statement to update the password field in the login table with encrypted data. But since sql 2005 does not allow users to mess with system tables, I am wondering if there are any ways to update user password without specifying the old password when I use "sa" user to login.
Hi, I have a report where i do a page break for each supplier. I want to reset my page number to '1' when there is a group break.When i googled for this functionality i found the following chris Hay's blog which provides the solution. http://blogs.msdn.com/chrishays/archive/2006/01/05/ResetPageNumberOnGroup.aspx But if i follow the same, I get #Error instead of page number during the run time.How do i resolve the error? Also, I want to display the "page 1 of totalpages in group" like that. Is that possible?
Is there a way that I can reset the key field to 1 when using DELETE to clear a table? (Note: if there is a separate command that I could use after the DELETE, that is fine too.) Thank you for your help with this, -DJ
Has anyone else experienced SQL Express resetting to Windows Authentication only instead of mixed mode for logins after a power failure. I only noticed it happen today as we had 2 power outages in the office and it happened both times. If anyone has seen this happen or knows of a fix please let me know.
I have four columns in my table, the first one is the identity column
col1 Col1 col2 col3
1 12 1 This is Test1 2 12 2 This is Test1 3 12 3 This is Test3 4 12 4 This is Test4 5 12 5 @@@@@
When, I see, @@@ sign in my col4, I need to restart the col3 from 1 again so it will look like this
col1 Col2 col3 col4
1 12 1 This is Test1 2 12 2 This is Test1 3 12 3 This is Test3 4 12 4 This is Test4 5 12 5 @@@@@ 6 12 1 This is another test1 7 12 2 This is another Test2
Hi all, I am trying to write a web page connected to an sql database of my MP3 files. I have a dropdown list box that populates with the artist, this hopefully being used to filter results into a datagrid. However, i am having a problem with the ddlb box repopulating on post back, i trawled the news groups and coded the population of the ddlb within an "if not page.postback" statement but to no avail. If i connect the ddlb to another sql database i have, the page reloads with the selctedvalue retained. So the theory is now that it is the SQL database, (is this a limitation of SQL express?) and i am having troubleshooting this. Here is the SQL of the databse: USE [mp3]GO/****** Object: Table [dbo].[Table_1] Script Date: 04/21/2008 20:40:21 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE TABLE [dbo].[Table_1]([Title] [nvarchar](50) NOT NULL,[Artist] [nvarchar](50) NULL,[Album] [nvarchar](50) NULL,[Year] [nvarchar](25) NULL,[Comment] [nvarchar](500) NULL,[TrackNumber] [nvarchar](10) NULL,[path] [nvarchar](300) NOT NULL,[genre] [nvarchar](25) NULL,[id] [int] IDENTITY(1,1) NOT NULL,CONSTRAINT [PK_Table_1_1] PRIMARY KEY CLUSTERED ([id] ASC)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]) ON [PRIMARY] Any help much appreciated.
Hello! Got a problem I hope some clever people can help me out with..
I have a web form that displays a set of records in a grid. The grid is "paged" according to a PageNum column, with a dropdown box to change pages and buttons allowing the items to be moved up or down a list within a page or moved between pages. So the backend table (simplified) looks something like this
The problem is when I want to delete a page - I need the page numbers to automatically resequence themselves, so for example, If I delete "Page 2" (i.e. delete rows where PageNum = 2), all items on "Page 3" become "Page 2" (and any items on "Page 4" become "Page 3" etc).
This has proved straightforward to when deleting an item from a particular page, and can resequence ItemNum thanks to a clever bit of code found on SQLteam.com:
DECLARE @intCounter int SET @intCounter = 0 UPDATE <Item Table> @intCounter = ItemNum = @intCounter + 1 WHERE Pagenumber = <Currently Selected Page>
However I haven't been able to adapt this to resequence the Page number, as this involves resequencing blocks of numbers. The closest I can get is:
DECLARE @intCounter int SET @intCounter = 1 UPDATE <Itemtable> SET @intCounter = PageNum = CASE WHEN @intCounter = PageNum - 1 THEN @intCounter + 1 WHEN @intCounter = PageNum - 2 THEN @intCounter + 1 ELSE @intCounter END
But this doesn't quite work. Anyone got any other ideas??
It seems to me that if a scheduled SSRS report subscription fails ( Status Message - An error has occurred during report processing) , that I actually need to delete the subscription and reconfigure it from scratch. The scheduled job doesn't try to run again automatically (say the next Monday on a weekly Monday schedule).
Is there a way to "reset" a failed subscription without have to recreate the entire subscription?
How to count the number of values that exist in a row based on the values from an array of numbers. Basically the the array of numbers I want to look for are in row 1 of table [test 1] and I want to search for them and count the "out of" in table [test 2]. Excuse me for not using the easiest way to convey my question below. I guess in short I have 10 numbers and like to find how many of those numbers exist in each row. short example:
I am trying to think my way through a solution which I believe others have probably come across... I am trying to implement a matching routine wherein I need to match an address against a high value and a low value (or, for that matter an input date vs. a start and end date) to return the desired row ... i.e. if I were to use a straight vb program I would just use the following lookup:
" WHERE zip_code = @zip_code AND addr_prim_lo <= @street_number AND addr_prim_hi >= @street_number " & _
" AND addr_prim_oe = @addr_prim_oe AND street_pre = @street_pre AND street_name = @street_name " & _
" AND street_suff = @street_suff AND street_post = @street_post " & _
" AND (expiry_date = '' OR expiry_date = '00000000' OR expiry_date > @expiry_date)" & _
" GROUP BY fire_ID, police_ID, fire_opt_in_out, police_opt_in_out"
My question, then, is how would you perform this type of query using a lookup / merge join or script? I have not found a way to implement a way to set the input columns? I can set the straight matches without a problem, i.e. lookup zip code = input zip code, but can't think of the correct way to set comparisons, i.e. lookup value 1 <= input value AND lookup value 2 >= input value