Guessing Data Type of columns stored as VarChar in SQLServer - sql

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!

Related

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/

Newly added table not working

I am working on DB2 9.7 database. I was using SquirrelSQL for GUI purpose. However ever since I applied an alter command to one of my tables , I started facing problems with the table, and any further select queries asked for "reorg" of the table. to overcome this, I renamed the old table and created new table. However the create query didn't execute properly in Squirrel ,and so downloaded DBViewer for my STS(Spring Source Tool Suite.)I executed the create table query from DBViewer, but the issue now is neither am I able to access the newly created table from my JAVA code , nor from Squirrel.
I am completely clueless as to what could be the problem. Has anyone got any idea?
Following is the Structure of my table:
CREATE TABLE DB2ADMIN.CERT
(
CERT_ID CHAR(36) NOT NULL,
CERT_CD CHAR(1) NOT NULL,
CERT_NBR CHAR(10) NOT NULL,
CERT_REQ_USR_CD_1 CHAR(10),
CERT_REQ_USR_CD_2 CHAR(10),
CERT_REQ_USR_CD_3 CHAR(10),
CERT_REQ_USR_CD_4 CHAR(10),
CERT_REQ_USR_TXT_1 VARCHAR(255),
CERT_REQ_USR_TXT_2 VARCHAR(255),
CERT_REQ_USR_TXT_3 VARCHAR(255),
CERT_REQ_USR_TXT_4 VARCHAR(255),
CERT_REQ_USR_TXT_5 VARCHAR(255),
CERT_REQ_USR_TXT_6 VARCHAR(255),
CERT_REQ_USR_TXT_7 VARCHAR(255),
CERT_REQ_USR_TXT_8 VARCHAR(255),
CERT_REQ_USR_TXT_9 VARCHAR(255),
CERT_REQ_USR_DT DATE,
LAST_MDF_USER_ID CHAR(25) NOT NULL,
LAST_MDF_ACY_TS TIMESTAMP(26,6) NOT NULL,
CONSTRAINT SQL111017085116710 PRIMARY KEY (CERT_ID)
);
In my alter query I changed the datatype of CERT_NBR form INTEGER to CHAR
using the command;
ALTER TABLE CERT ALTER COLUMN CERT_NBR SET DATA TYPE INTEGER
any further select queries asked for "reorg" of the table.
This is a common occurrence after altering tables in DB2. It should be trivially solved by calling:
reorg table table-name;
This is a command-line command, rather than an sql statement, but you can call it via SQL with the admin_cmd procedure:
call sysproc.admin_cmd('reorg table table-name');
I'm not sure why you are unable to access the new table. The error message should help you resolve this. Some possibilities:
You created it with a different username than the one you are trying to access it with.
You never committed after the create table statement.
Something went wrong and the table ended up in an inoperable state.

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...

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?

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

Operand type clash using user defined table type on SQL Server

I have created a user defined type (with script to CREATE) given below:
CREATE TYPE [dbo].[udt_ProductID_SellingPrice] AS TABLE(
[Product_Id] [int] NOT NULL,
[Selling_Price] [money] NULL,
PRIMARY KEY CLUSTERED
(
[Product_Id] ASC
)WITH (IGNORE_DUP_KEY = OFF)
)
On the same database I have created a function that uses this type:
CREATE FUNCTION [dbo].[fn_NetGP_tbl](#InputSKUs udt_ProductID_SellingPrice READONLY, #Currency VARCHAR(3), #SiteName VARCHAR(255), #CatalogueSite VARCHAR(5), #SiteGroup VARCHAR(255))
RETURNS #Results TABLE
(
Product_Id INT NOT NULL,
Selling_Price MONEY NULL,
Net_GP MONEY NULL
)
AS
BEGIN
I declare the Input SKUs as an in-memory table as follows and call the function as follows:
DECLARE #InputSKUs dbo.udt_ProductId_SellingPrice
INSERT INTO #InputSKUs VALUES(10352316, 500.00)
SELECT * FROM Sensu_Staging.dbo.fn_NetGP_tbl(#InputSKUs, 'GBP', 'Site_Name', 'SITE', 'Site_Group')
And get the (what I'm finding, fairly confusing) error message:
Operand type clash: udt_ProductID_SellingPrice is incompatible with udt_ProductID_SellingPrice
I can't really work out what I'm doing wrong - this has worked on previous databases, the only change I've made is adding a primary key/clustered index to ProductID_SellingPrice type and this seems to have thrown everything off.
Do I need to change the function? Or is it not possible to create an index on an in-memory custom table type? We were hoping to speed the process up by indexing that table but if it's not possible then I guess we'll have to find other ways.
Cheers,
Matt
I had previously defined the user data type in a different database and had referenced it explicitly in the functions I was calling (the Net GP function in this case).
I had then changed the data type to include a primary key (both with the same name) and yet was still referencing the previous data type in the body of my function hence the confusing, but entirely accurate error message.
In short, make sure that you either change all references at all points in your function or, more sensibly, call your data types different things so as to be able to troubleshoot this better!