How can I reseed a table for Merge replication when it has reach its max identity value. Software is hard coded as int so cannot change datatype? - sql

I have a client using SQL 2008R2 and they have Merge replication in place. They have hit the max range on a few of their tables because the subscriptions were being removed and readded multiple times. The table on the publisher only contains 175000 row and is replicating to 7 other sites so they should be no where close to hitting the max.
How can I reseed the table and keep all the data intact? I tried copying the table and then dropping the table and renaming it but the identity ranges values stay the same. I cannot change the data type because the uses of Int as a datatype is hard coded into our software.
Any help would be appreciated.
Table looks like this and the rownum is the id column.
rowguid, uniqueidentifier,
,contactid, float,
,RowNum, int,
,type, int,
,createdby, varchar(30),
,assignedto, varchar(30),
,createddate, int,
,modifieddate, int,
,startdate, int,
,duedate, int,
,completedate, int,
,duration, int,
,tickler, int,
,priority, smallint,
,therule, int,
,status, int,
,private, tinyint,
,flags, int,
,subject, int,
,notes, text

Related

Why am I getting "Error converting data type varchar to int."

I am creating a mock database for a fictional gym. I have 3 relevant tables: Program, Workouts and Exercises (see below). Each Program contains one or many Workouts and each Workout contains one or many Exercises. I am trying to create a stored procedure to add rows to these three tables. I keep getting the error "Error converting data type varchar to int.". I can't understand why because the variable datatypes all match the datatypes in the tables (as far as I can see).
CREATE TABLE [Program]
(
[ProgramID] INT IDENTITY(1,1),
[MemberNumber] INT,
[TrainerID] INT,
[ProgramStartDate] DATE,
[TrainerReviewDate] DATE,
[Active] CHAR(1),
PRIMARY KEY ([ProgramID]),
FOREIGN KEY ([MemberNumber])
REFERENCES [Members] ([MemberNumber])
ON DELETE SET NULL ON UPDATE CASCADE,
FOREIGN KEY ([TrainerID])
REFERENCES [Trainers] ([TrainerID])
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE [Workouts]
(
[WorkoutID] INT IDENTITY(1,1),
[Description] VARCHAR(50),
[Day] VARCHAR(10),
[ProgramID] INT,
PRIMARY KEY ([WorkoutID]),
FOREIGN KEY ([ProgramID])
REFERENCES [Program] ([ProgramID])
ON DELETE SET NULL ON UPDATE CASCADE
);
CREATE TABLE [Exercise]
(
[ExerciseID] INT IDENTITY(1,1),
[ExerciseType] VARCHAR(30),
[Equipment] VARCHAR(30),
[Sets] INT,
[Reps] INT,
[WorkoutID] INT,
PRIMARY KEY ([ExerciseID]),
FOREIGN KEY ([WorkoutID])
REFERENCES [Workouts] ([WorkoutID])
ON DELETE SET NULL ON UPDATE CASCADE
)
/*Description: This stored procedure adds a new program with workout/exercise details. */
CREATE PROCEDURE usp_New_Program_4 (
#MemberNumber int,
#TrainerID int,
#ProgramStartDate date,
#TrainerReviewDate date,
#Active char(1),
#Description varchar(50),
#Day varchar(10),
#ProgramID int = SCOPE_IDENTITY,
#ExerciseType_1 varchar(30),
#Equipment_1 varchar(30),
#Sets_1 int,
#Reps_1 int,
#ExerciseType_2 varchar(30),
#Equipment_2 varchar(30),
#Sets_2 int,
#Reps_2 int,
#ExerciseType_3 varchar(30),
#Equipment_3 varchar(30),
#Sets_3 int,
#Reps_3 int,
#ExerciseType_4 varchar(30),
#Equipment_4 varchar(30),
#Sets_4 int,
#Reps_4 int,
#WorkoutID int = SCOPE_IDENTITY
)
AS
SET xact_abort ON
BEGIN
INSERT INTO Program (MemberNumber, TrainerID, ProgramStartDate, TrainerReviewDate, Active)
VALUES (#MemberNumber, #TrainerID, #ProgramStartDate, #TrainerReviewDate, #Active);
INSERT INTO Workouts (Description, Day, ProgramID)
VALUES (#Description, #Day, SCOPE_IDENTITY());
INSERT INTO Exercise (ExerciseType, Equipment, Sets, Reps, WorkoutID)
VALUES (#ExerciseType_1, #Equipment_1, #Sets_1, #Reps_1, SCOPE_IDENTITY()),
(#ExerciseType_2, #Equipment_2, #Sets_2, #Reps_2, SCOPE_IDENTITY()),
(#ExerciseType_3, #Equipment_3, #Sets_3, #Reps_3, SCOPE_IDENTITY()),
(#ExerciseType_4, #Equipment_4, #Sets_4, #Reps_4, SCOPE_IDENTITY())
END;
GO
/*Test for usp_New_Program_4
Result: Failed - Error converting data type varchar to int*/
Exec usp_New_Program_4 21,3,'2020-06-06','2020-07-07','Y','Chest & Arms',SCOPE_IDENTITY,'Bench
Press','50kg Barbell',3,6,'Press Ups','Floor Mat',3,15,'Bicep Curls','15kg
Dumbells',3,6,'Tricep Extensions','15kg Dumbells',3,6,SCOPE_IDENTITY
Go
It looks like you may have missed a field in your line to execute the stored procedure. I quickly copied the stored proc input values into a spreadsheet followed by the values in your test line that throws the error. Have a look, it looks like you're missing perhaps the Day value? After that is entered it should line everything up correctly. I marked up the first line that after a quick glance looks like it is causing the error (others lower will too though, until the missing value is fixed).
Basic debugging would be to check your parameters... And if we do that...
Exec usp_New_Program_4 21, --#MemberNumber int
3, --#TrainerID int
'2020-06-06', --#ProgramStartDate date
'2020-07-07', --#TrainerReviewDate date
'Y', --#Active char(1)
'Chest & Arms', --#Description varchar(50)
SCOPE_IDENTITY, --#Day varchar(10)
'Bench
Press', --#ProgramID int
'50kg Barbell', --#ExerciseType_1 varchar(30)
3, --#Equipment_1 varchar(30)
6, --#Sets_1 int
'Press Ups', --#Reps_1 int
'Floor Mat', --#ExerciseType_2 varchar(30)
3, --#Equipment_2 varchar(30)
15, --#Sets_2 int
'Bicep Curls', --#Reps_2 int
'15kg
Dumbells', --#ExerciseType_3 varchar(30)
3, --#Equipment_3 varchar(30)
6, --#Sets_3 int
'Tricep Extensions', --#Reps_3 int
'15kg Dumbells', --#ExerciseType_4 varchar(30)
3, --#Equipment_4 varchar(30)
6, --#Sets_4 int
SCOPE_IDENTITY --#Reps_4 int
Pretty clear now. The value 'Bench
Press' is not an int. Neither are 'Press Ups'
, 'Bicep Curls' or 'Tricep Extensions'.
This is why using the format EXEC PROC {Proc Name} #{Parameter Name} = {Parameter Value} is really important for any procedures that have more than 1 or 2 parameters; you don't make simple mistakes like missing a parameter out.
In this case, you clearly missed out a value for #Day (as I assume you wanted to pass SCOPE_IDENTITY for #ProgramID), and thus all your parameters after that are wrong. The SP still ran fine though, as your last parameter (#WorkoutID) has a default value, and so didn't "mind" being omitted.

Adding Columns to Multiple Tables in SQL

I just created a database and then added a couple of hundred tables with a script like this:
CREATE TABLE CapBond
(
[timestamp] varchar(50),
[Reward] varchar(50),
[Award] varchar(50),
[Fact] varchar(50)
)
CREATE TABLE Values
(
[timestamp] varchar(50),
[Name] varchar(50),
[Test] varchar(50),
[Read] varchar(50),
[Parameters] varchar(50)
)
I realize I forgot to add two columns to each table. One for the PK and one for an FK that points back to a 'master' table.
Is there an easy way to insert columns without having to drop the DB and recreate it? Preferably with the columns inserted as the first two columns in the table?
Yes. In mysql you have the alter table command for this purpose. Check out this page for more detailed explanation
https://www.sqlservertutorial.net/sql-server-basics/sql-server-alter-table-add-column/ .
And here is the solution for the ordering of the columns
https://www.mysqltutorial.org/mysql-add-column/

Guessing Data Type of columns stored as VarChar in SQLServer

I'm just wondering if anyone has created or heard of a function to guess the appropriate data type of data stored in a SQLServer table as VarChar.
I'm working on an SSIS package that you can point at a directory, and it will loop through and create tables / import data for every CSV that exists it. I'm having trouble with specifying the data types before import, so as a work around I would like to import all the data as VarChar(50) into a temporary table and then run some sort of function to analyze each column for the appropriate data type (int, decimal, float, etc) so I can use that to script the create table and insert statements.
So for example I'd like to be able to point a function or query at this temp table
CREATE TABLE [#Data]
(
[ProductCode] varchar(50),
[ProductName] varchar(50),
[Year] varchar(50),
[Total_volume] varchar(50),
[Total_Quantity] varchar(50),
[PercentSold] varchar(50)
)
to read through the data and determine what data type / length is most appropriate - much like the 'Suggest Data Type' tool in Excel Connection Manager does, only something I can tie into a variable to be done dynamically. It should end up looking something like
CREATE TABLE [Data]
(
[ProductCode] varchar(6),
[ProductName] varchar(11),
[Year] int,
[Total_volume] int,
[Total_Quantity] int,
[PercentSold] decimal(3,2)
)
Any thoughts?
Thanks!

Autogenerate ID in stored procedure -SQL

I have this stored procedure in SQL Server which I use in visual net to generate values for some table.
CREATE PROCEDURE grabardatos
#codigocliente int,
#nombre varchar(50),
#apellido varchar(50),
#sexo char(1),
#direcion varchar(50),
#telefono varchar(50),
#tipo varchar(50)
AS
BEGIN
INSERT INTO CLIENTES(Numero,Nombre,Apellido,Sexo,Direcion,Telefono,Tipo)
VALUES ( #codigocliente,#nombre,#apellido,#sexo,#direcion,#telefono,#tipo)
END
The parameter #codigocliente is the id of the table which, according to this code, has to be entered manually in visual net. How Can the id be autogenerated in the sql code of the stored procecure instead of being entered manually in visual net?
If the clientes table has an identity key, it will be automatically generated by SQL as part of the INSERT. You can then use ##identity to retrieve it's value.
The add the key to the table, you need to create a column
ALTER TABLE Add IdKey INT IDENTITY(1,1)
Note that during the INSERT, you cannot provide a value for this column...

why can't I insert data into tables I created in sql server managment studio 2012? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I created the tables with this:
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40)
DESTINATION VARCHAR(40)
);
CREATE TABLE TAXI(
TAXI_ID INT,
COSTUMER_ID INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_PHONE_NUMBER INT,
DRIVER_NAME INT,
DESTINATION
);
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
);
After this my table appeared with all the attributes. Then I set my PKs and FKs manually.
now am trying to insert some data to each of the tables but I keep getting error 213:
Column name or number of supplied values does not match table definition
I searched and searched and still don't understand.
also when I insert the code for such as SELECT * FROM Costumer (that's the table name) it won't show in the small suggestion box as if it doesn't exist???
The below code should work for your requirement unless you have specified an identity column manually as you said that you made some changes manually.
In case you specified an identity column manually then you should not insert the value for the Identity column (say Customer_ID) or you should set SET IDENTITY_INSERT COSTUMER ON before inserting the ID column
CREATE TABLE COSTUMER(
COSTUMER_ID INT,
TAXI_ID INT,
COSTUMER_PHONE_NUMBER INT,
COSTUMER_NAME VARCHAR(40),
DESTINATION VARCHAR(40)
);
SELECT * FROM COSTUMER
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID,COSTUMER_PHONE_NUMBER,COSTUMER_NAME,DESTINATION) VALUES (1, 33,123211241, 'John','Mexico' )
I think you are trying to insert more/less input to table
INSERT INTO [TableName] (Column1, Column2, Column3)
VALUES (Value1, Value2, Value3)
Number of columns should be equal to Number of Values. Please check your INSERT statement.
If you not included (Column1, Column2, Column3) in your statement, include it.
Post the insert statement you used. I see a few problems with the current code you posted.
You don't provide datatypes for a few of your attributes, for instance in the Booking table you need to provide some type for the CUSTOMER_ID attribute. Also you need the attributes and datatypes to be comma separated, except the last one doesn't need a trailing comma. For instance you have:
...
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40)
COSTUMER_NAME VARCHAR(40)
TIME_OF_ARRIVAL DATETIME,
...
This will throw an error probably saying incorrect syntax near COSTUMER_NAME.
You need something like:
CREATE TABLE BOOKING(
DESTINATION VARCHAR(40),
TAXI_ID INT,
COSTUMER_ID INT,
COSTUMER_PHONE_NUMBER INT,
DRIVER_PHONE_NUMBER INT,
TAXI_REGISTRATION_NUMBER INT,
TAXI_NAME VARCHAR(40),
COSTUMER_NAME VARCHAR(40),
TIME_OF_ARRIVAL DATETIME
);
For an insert try something along the lines of:
INSERT INTO <table_name> (<column1>,<column2>,<column3>)
VALUES (<col1Value>,<col2Value>,<col3Value>)
If you aren't getting auto completion you might try and refresh the DB in the management studio by right click > refresh, but I wouldn't rely on auto completion at all in SQL Server nor IntelliSense unless you use someting like this.
If you post the insertion script I might be able to help more.
Update: So, the problem you got was with arithmetic overflow, specifically trying to put 07854625781 into a int. All datatypes have maximum/minimums or other "constraints", an int can be in the range of -2^31 (-2,147,483,648) through 2^31 - 1 (2,147,483,647) (4 bytes). The value you enter exceeds that, it is being interpreted as 07,854,625,781 (7.8 billion) while the maximum for an int is only 2.147 billion. If you change the datatype of phone number to bigint that can hold -2^63 (-9,223,372,036,854,775,808) through 2^63-1 (9,223,372,036,854,775,807) (8 bytes) it will work, However I would advise you not to store a phone number in that way, rather, use char(length) if you know that the size and format will always be the same, if you think the length could vary (maybe from extensions or international numbers) I would use something along the lines of varchar(40).
Through this datatype change you will just have to surround input in 'single quotes' when you insert.
Cheers
The Problem that you are facing in the below insert query is the value 07854625781 has become too large to handle for int type. Int range 2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647). So change the datatype of COSTUMER_PHONE_NUMBER to Bigint.
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport')
use the below query to change the datatype to bigint and then try insert(above command)
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
Steps to Follow
ALTER TABLE COSTUMER ALTER COSTUMER_PHONE_NUMBER bigint;
INSERT INTO COSTUMER (COSTUMER_ID, TAXI_ID, COSTUMER_PHONE_NUMBER, COSTUMER_NAME, DESTINATION) values (1, 32, 07854625781, 'Denzel Washington', 'Heathrow Airport');
Now do
Select * from COSTUMER
you will find the record inserted