TSQL Help (SQL Server 2005) - sql-server

I have been playing around with a quite complex SQL Statement for a few days, and have gotten most of it working correctly.
I am having trouble with one last part, and was wondering if anyone could shed some light on the issue, as I have no idea why it isnt working:
INSERT INTO ExistingClientsAccounts_IMPORT
SELECT DISTINCT
cca.AccountID, cca.SKBranch, cca.SKAccount, cca.SKName, cca.SKBase,
cca.SyncStatus, cca.SKCCY, cca.ClientType, cca.GFCID, cca.GFPID, cca.SyncInput,
cca.SyncUpdate, cca.LastUpdatedBy, cca.Deleted, cca.Branch_Account, cca.AccountTypeID
FROM ClientsAccounts AS cca
INNER JOIN
(SELECT DISTINCT ClientAccount, SKAccount, SKDesc,
SKBase, SKBranch, ClientType, SKStatus, GFCID,
GFPID, Account_Open_Date, Account_Update
FROM ClientsAccounts_IMPORT) AS ccai
ON cca.Branch_Account = ccai.ClientAccount
Table definitions follow:
CREATE TABLE [dbo].[ExistingClientsAccounts_IMPORT](
[AccountID] [int] NOT NULL,
[SKBranch] [varchar](2) NOT NULL,
[SKAccount] [varchar](12) NOT NULL,
[SKName] [varchar](255) NULL,
[SKBase] [varchar](16) NULL,
[SyncStatus] [varchar](50) NULL,
[SKCCY] [varchar](5) NULL,
[ClientType] [varchar](50) NULL,
[GFCID] [varchar](10) NULL,
[GFPID] [varchar](10) NULL,
[SyncInput] [smalldatetime] NULL,
[SyncUpdate] [smalldatetime] NULL,
[LastUpdatedBy] [varchar](50) NOT NULL,
[Deleted] [tinyint] NOT NULL,
[Branch_Account] [varchar](16) NOT NULL,
[AccountTypeID] [int] NOT NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[ClientsAccounts_IMPORT](
[NEWClientIndex] [bigint] NOT NULL,
[ClientGroup] [varchar](255) NOT NULL,
[ClientAccount] [varchar](255) NOT NULL,
[SKAccount] [varchar](255) NOT NULL,
[SKDesc] [varchar](255) NOT NULL,
[SKBase] [varchar](10) NULL,
[SKBranch] [varchar](2) NOT NULL,
[ClientType] [varchar](255) NOT NULL,
[SKStatus] [varchar](255) NOT NULL,
[GFCID] [varchar](255) NULL,
[GFPID] [varchar](255) NULL,
[Account_Open_Date] [smalldatetime] NULL,
[Account_Update] [smalldatetime] NULL,
[SKType] [varchar](255) NOT NULL
) ON [PRIMARY]
The error message I get is:
Msg 8152, Level 16, State 14, Line 1
String or binary data would be truncated.
The statement has been terminated.

The error is because you are trying to insert data into a column in ExistingClientsAccounts_IMPORT where the column size is smaller than the length of data attempting to be inserted into it.
e.g.
SKAccount column is VARCHAR(12) in the ExistingClientsAccounts_IMPORT table but is VARCHAR(255) in ClientsAccounts_IMPORT.
So if ClientsAccounts_IMPORT contains any rows where that field is longer than 12 characters, you will get that error as obv. e.g. 100 characters will not fit into a 12 character field.
You need to make sure all the columns in the table you are inserting into, are big enough - make sure each column definition matches the source table.

The third column of your SELECT column list means that ExistingClientsAccounts_IMPORT.SKAccount is populated from ClientsAccounts.SKAccount - however, the source is up to 255 characters, while the destination has a capacity of 12. If there's any data that wouldn't fit, you'll get this message.
I haven't gone through all the other columns.

You are trying to insert values which are greater than tha max length specified for a column. Use a profiler to check the data being passed to this query and verify the length of data against the permissible length for all columns.
There is a clear mismatch in the column lenghts of common columns of these two tables.
ClientsAccounts_IMPORT.SKBase is 10 whereas it is 16 in the other table.

Check the field definitions. You can see some are smaller than the original ones. Now run a query on the old data - you will find some of the larger fields were used, so the insert is not possible without losing data.
Example: SKAccount - from 255 length to 12.

Related

MS Access Sequence jumped when invalid data is entered

I have created a SQL Server Sequence (Backend)
SEQUENCE
CREATE SEQUENCE [dbo].[Airlines_Mast_SEQ]
AS [int]
START WITH 1
INCREMENT BY 1
MINVALUE 1
MAXVALUE 99999
CACHE
GO
TABLE
CREATE TABLE [dbo].[Airlines_Mast](
[ID] [int] NOT NULL DEFAULT NEXT VALUE FOR [Airlines_Mast_SEQ] PRIMARY KEY,
[Airlines_ID] AS ('AS'+right('00000'+CONVERT([varchar],[ID]),(6))) PERSISTED NOT NULL,
[Airlines_CODE] [varchar](4) NOT NULL,
[Airlines_Name] [nvarchar](100) NOT NULL,
[Airlines_GSTIN] [varchar](15) NOT NULL,
[Airlines_Address1] [nvarchar](100) NULL,
[Airlines_Pincode] [varchar](10) NULL,
[Legal_Name] [nvarchar](100) NULL,
[Type] [varchar](15) NULL,
[Airlines_Active] [bit] NOT NULL
) ON [PRIMARY]
MS Access(Front End)
I have created a Form in MS Access using table Airlines_Mast , while entering data in Form
(1) When Data is correctly entered for a Record in one Go then SEQUENCE is maintained but,
(2) When invalid Data is entered multiple time for a Record after validation check then SEQUENCE got jumped to next sequence.
Kindly suggest to maintain the sequence without Jump.

Cannot create a row of size 8081 which is greater than the allowable maximum row size of 8060

I checked every link of stackoverflow (I Rebuild table too) and others link but no luck.
I have to analyze one application. So I I got backup file, via application utility on particular path. I restored backup file in Sqlserver 2014 full version properly. Then I change the config file of the application to this sqlserver.
After that, when run the application and submit employee form via applicaion its gives the above error.
Employee table db structure as below, it contains 47 columns and does not have any rows( This is first entry).
CREATE TABLE [dbo].[employee](
[wcsuid] [nchar](30) NULL,
[lastupdatedate] [nchar](8) NULL,
[updatedatetime] [varchar](17) NULL,
[locationid] [nchar](20) NULL,
[isactive] [nchar](1) NULL,
[CreatedDate] [varchar](10) NULL,
[sdate] [nchar](90) NULL,
[identityemployeeno] [nchar](45) NULL,
[employeeid] [nchar](180) NULL,
[firstname] [nchar](120) NULL,
[lastname] [nchar](120) NULL,
[ssn] [nchar](99) NULL,
[dob] [nchar](90) NULL,
[gender] [nchar](9) NULL,
[address] [nchar](180) NULL,
[addressLine2] [nchar](180) NULL,
[city] [nchar](105) NULL,
[state] [nchar](180) NULL,
[zipcode] [nchar](135) NULL,
[isinternationaladdress] [nchar](9) NULL,
[telephone] [nchar](126) NULL,
[cellphone] [nchar](126) NULL,
[IdentityJobTitleNo] [nchar](45) NULL,
[manager] [nchar](120) NULL,
[hiredate] [nchar](90) NULL,
[terminationdate] [nchar](90) NULL,
[username] [nchar](90) NULL,
[userpassword] [nchar](90) NULL,
[userpassword_required] [nchar](9) NULL,
[employeestanding] [nchar](180) NULL,
[identityroleid] [nchar](45) NULL,
[loggedin] [nchar](9) NULL,
[punchedin] [nchar](9) NULL,
[punchedinTime] [nchar](90) NULL,
[punchedinDate] [nchar](90) NULL,
[contactname] [nchar](120) NULL,
[contactaddress] [nchar](180) NULL,
[contactaddressLine2] [nchar](180) NULL,
[contactcity] [nchar](105) NULL,
[contactstate] [nchar](30) NULL,
[contactzipcode] [nchar](135) NULL,
[isinternationalcontactaddress] [nchar](9) NULL,
[contactphone] [nchar](126) NULL,
[contactcellphone] [nchar](126) NULL,
[relationtoemployee] [nchar](180) NULL,
[worksoncommission] [nchar](9) NULL,
[overrideitemcommission] [nchar](9) NULL
) ON [PRIMARY]
And insert statement (also have 47 values) with error also show in application as below :
INSERT INTO employee
(
wcsuid,lastupdatedate,updatedatetime,locationid,isactive,CreatedDate,sdate,identityemployeeno,employeeid,firstname,lastname,ssn,dob,gender,address,addressLine2,city,state,zipcode,isinternationaladdress,telephone,cellphone,IdentityJobTitleNo,manager,hiredate,terminationdate,username,
userpassword,userpassword_required,employeestanding,identityroleid,loggedin,punchedin,punchedinTime,punchedinDate,contactname,contactaddress,contactaddressLine2,contactcity,contactstate,contactzipcode,isinternationalcontactaddress,contactphone,contactcellphone,relationtoemployee,worksoncommission,overrideitemcommission)
VALUES
(
'2015121512201580210002','','20151215122015802','','','20151215','','4','','Ajayendra','Raghuvanshi','','19782707','M','','','','','','','0792762063','','','','00000000','00000000','ajayendra',
'SdYHcqaxf1gMjjSkjmpUiw==','N','','','','','','','','','','','','','','','','','N','N')
Is the limit of char fields, but I checked all length with the values and does not get any logic behind error.
So question is How to solved the error and why it is comes?
I am not an SQL Server expert but... Adding all the field length above I got 4,046 (I might have made few mistakes - it is too early here and I am still on the first coffee...). Now looking at the manual:
nchar [ ( n ) ]
Fixed-length Unicode string data. n defines the string length and must be a value from 1 through 4,000. The storage size is two times n bytes. When the collation code page uses double-byte characters, the storage size is still n bytes. Depending on the string, the storage size of n bytes can be less than the value specified for n. The ISO synonyms for nchar are national char and national character..
Note that storing each characters requires two bytes. Doing 2 * 4046 we have 8092 bytes line length! Also note that nchar is fixed length which I assume is allocated when an entry/row is created and thus it generates the error that you see, telling you that the line is too long. I would expect it to warn you at least when you create the table...
Solutions
normalize more: Split the table into two, for example have an emploeeyAddress table which can make sense if you later need home and work addresses for each employee
Use nvarchar which is variable or dynamic length. The storage is still 2 bytes per character but only for the data inserted (not pre-allocated). You will hit the same limit if you try to populate all fields to their maximum length
For nchar values, each character uses up two bytes, so you should multiply your character count by two to get the total row size.
Finally I got the solution. I am in analyze phase, so I can not change in normalize table.
After reading this below link, I understand the issue. So I just recreated a "Employee" table with nvarchar datatype as I have only option.
Thanks #urban to give the right direction and for this I accept his answer.
What is the difference between char, nchar, varchar, and nvarchar in SQL Server?

TSQL Large Data Set Merge

I have two data sets (190 Million Rows and 100 Million Rows). I need to find the most effective way to merge the two together with no duplicate rows. I can do this via TSQL commands or an SSIS process. Does anyone have any input/experience as to what the most effective way to complete this process is?
Both tables have the same formatting:
CREATE TABLE [dbo].[Table01](
[StudentId] [char](10) NOT NULL,
[CollegeId] [char](6) NOT NULL,
[TermId] [char](3) NOT NULL,
[CourseId] [char](12) NULL,
[Title] [char](68) NULL,
[SectionId] [char](6) NULL,
[UnitsEarned] [decimal](5, 2) NULL,
[Grade] [char](3) NULL,
[CreditFlag] [char](1) NULL,
[UnitsAttempted] [decimal](5, 2) NULL,
[TopCode] [char](6) NULL,
[TransferStatus] [char](1) NULL,
[UnitsMax] [decimal](5, 2) NULL,
[BSStatus] [char](1) NULL,
[SamCode] [char](1) NULL,
[ClassCode] [char](1) NULL,
[CollegeLevel] [char](1) NULL,
[NCRCategory] [char](1) NULL,
[CCLongTermId] [char](5) NULL,
[batch_id] [int] NULL
)
These are the fields that need to be distinct to eliminate duplicates:
[StudentId]
[CollegeId]
[TermId]
[CourseId]
The server that will be running this process has 8 cores, 32GB RAM, and SQL Server 2012.
Create a composite clustered index on all 4 columns on both tables. Create the destination table with a standard identity field primary key clustered index, but with a composite nonclustered index on those 4 columns. Insert into destination table from table01, using a derived table that also exposes ROW_NUMBER() OVER (PARTITION BY StudentId, CollegeId, TermId, CourseId), and filter out all rows except for those with ROW_NUMBER of 1. That will dedupe table01. Then do the same thing with table02, but also using a NOT EXISTS to check against the destination table to make sure that a row does not already exist.

Maximum row size of 8060 Error

I am stuck and I can not figure out. Does anyone have any ideas? I would greatly appreciate any help.
I have a table in SQL Server 2008 with the following defintion:
I can insert/delete/update without any error. However, when i run a select statement that filter out ONE single item, i got the following error:
Msg 511, Level 16, State 1, Line 1
Cannot create a row of size 8104 which is greater than the allowable maximum row size of 8060.
If I remove the where condition a15.MEMBER_PARENT_LEVEL_ID4 in (91329) it ran fine. If I add 5 or 10 criterias it ran fine, including the one that is throwing the error if you add it by itself.
[MSTR_PROD_HIER_ID] [int] NOT NULL,
[MSTR_PROD_HIER_DESC] [varchar](50) NULL,
[MEMBER_GROUP_LEVEL_ID1] [int] NULL,
[MEMBER_GROUP_LEVEL_DESC1] [varchar](60) NULL,
[MEMBERKEY_SORT1] [int] NULL,
[MEMBER_GROUP_LEVEL_ID2] [int] NULL,
[MEMBER_GROUP_LEVEL_DESC2] [varchar](60) NULL,
[MEMBERKEY_SORT2] [int] NULL,
[MEMBER_GROUP_LEVEL_ID3] [int] NULL,
[MEMBER_GROUP_LEVEL_DESC3] [varchar](60) NULL,
[MEMBERKEY_SORT3] [int] NULL,
[MEMBER_LEVEL_ID4] [int] NULL,
[MEMBER_LEVEL_DESC4] [varchar](60) NULL,
[MEMBER_LEVEL_DESC4_1] [varchar](60) NULL,
[MEMBER_PARENT_LEVEL_ID1] [int] NOT NULL,
[MEMBER_PARENT_LEVEL_DESC1] [varchar](60) NULL,
[SORT1] [int] NULL,
[MEMBER_PARENT_LEVEL_ID2] [int] NULL,
[MEMBER_PARENT_LEVEL_DESC2] [varchar](60) NULL,
[SORT2] [int] NULL,
[MEMBER_PARENT_LEVEL_ID3] [int] NULL,
[MEMBER_PARENT_LEVEL_DESC3] [varchar](60) NULL,
[SORT3] [int] NULL,
[MEMBER_PARENT_LEVEL_ID4] [int] NULL,
[MEMBER_PARENT_LEVEL_DESC4] [varchar](60) NULL,
[SORT4] [int] NULL,
[CLASS_DESC] [varchar](60) NULL,
[CLASS_DESC1] [varchar](50) NULL,
[TOTAL_BRAND_IND] [int] NOT NULL
}
{
select a11.PlanFormat PlanFormat,
max(a18.FormatName) FormatName,
a15.MEMBER_PARENT_LEVEL_ID2 MEMBER_PARENT_LEVEL_ID2,
max(a15.MEMBER_PARENT_LEVEL_DESC2) MEMBER_PARENT_LEVEL_DESC2,
a15.MEMBER_PARENT_LEVEL_ID3 MEMBER_PARENT_LEVEL_ID3,
max(a15.MEMBER_PARENT_LEVEL_DESC3) MEMBER_PARENT_LEVEL_DESC3,
a15.MEMBER_PARENT_LEVEL_ID4 MEMBER_PARENT_LEVEL_ID4,
max(a15.MEMBER_PARENT_LEVEL_DESC4) MEMBER_PARENT_LEVEL_DESC4,
max(a15.CLASS_DESC) CLASS_DESC,
max(a15.CLASS_DESC1) CLASS_DESC1,
a16.MSTR_SEASON_ID MSTR_SEASON_ID,
max(a16.MSTR_SEASON_DESC) MSTR_SEASON_DESC,
max(a16.MSTR_SEASON_DESC1) MSTR_SEASON_DESC1,
a17.MSTR_STORE_ID MSTR_STORE_ID,
max(a17.MSTR_STORE_DESC1) MSTR_STORE_DESC1,
a13.MSTR_C_MNTH_ID MSTR_C_MNTH_ID,
max(a13.MSTR_C_MNTH_DESC) MSTR_C_MNTH_DESC,
a13.MSTR_CALENDAR_GRP_ID MSTR_CALENDAR_GRP_ID,
max(a13.MSTR_CALENDAR_GRP_DESC) MSTR_CALENDAR_GRP_DESC,
a12.YEAR_NBR YEAR_NBR,
a11.PlanNo PlanNo,
max(a18.PlanName) PlanName,
sum((a11.Value1821 / 1000.0)) ACT_PROJ_SLS_DOL
from A_MSTR_PLAN_FCT a11
join A_MSTR_CALENDAR_MONTH_XREF a12
on (a11.Member2 = a12.Member2)
join A_MSTR_CALENDAR_MONTH a13
on (a12.DIM_TIME_ID = a13.DIM_TIME_ID and
a12.YEAR_NBR = a13.YEAR_NBR)
join A_MSTR_PRODUCT_LEVEL_3 a14
on (a11.MEMBER0 = a14.P_MEMBER3)
join A_MSTR_PRODUCT_LEVEL_3_PARENT a15
on (a14.MEMBER_LEVEL_ID4 = a15.MEMBER_LEVEL_ID4 and
a14.MSTR_PROD_HIER_ID = a15.MSTR_PROD_HIER_ID)
join A_MSTR_SEASONALITY a16
on (a11.MEMBER3 = a16.MEMBER3)
join A_MSTR_STORE a17
on (a11.MEMBER1 = a17.MEMBER1)
join A_MSTR_PLAN a18
on (a11.PlanNo = a18.PlanNo and
a12.YEAR_NBR = a18.YEAR_NBR and
a13.YEAR_ID = a18.YEAR_ID)
where (a11.PlanNo in (1547)
and a15.MEMBER_PARENT_LEVEL_ID4 in (91329))
group by a11.PlanFormat,
a15.MEMBER_PARENT_LEVEL_ID2,
a15.MEMBER_PARENT_LEVEL_ID3,
a15.MEMBER_PARENT_LEVEL_ID4,
a16.MSTR_SEASON_ID,
a17.MSTR_STORE_ID,
a13.MSTR_C_MNTH_ID,
a13.MSTR_CALENDAR_GRP_ID,
a12.YEAR_NBR,
a11.PlanNo
}
This is a follow up. to my previous post. As i mentioned below, this error goes away after i dropped the table, perform DBCC clean up and recreate the table. Now, it is coming back but this time, only when you select two or more criterias. The problem seems to be with the where condition where it filter out my product. My product table does not have any nvarchar or varbinary.
I am so frustrated... :-(
I would suggest modifying the query to remove the IN clause(s) and replace it with additional joins or a WHERE EXISTS. This may generate a more efficient query plan which avoids the problem you're seeing.
If your physical data size is less than 8060 then too you are getting this error, try following command.
DBCC CLEANTABLE
For example:
DBCC CLEANTABLE (Databasename,'tablename', 0)
WITH NO_INFOMSGS;

SQL Server BULK INSERT FROM different schemas

I have a database that can have data updated from two external parties.
Each of those parties sends a pipe delimited text file that is BULK INSERTED into the staging table.
I now want to change the scheme for one of the parties by adding a few columns, but this is unfortunately breaking the BULK INSERT for the other party even though the new columns are all added as NULLABLE.
Is there any obvious solution to this?
TABLE SCHEMA:
CREATE TABLE [dbo].[CUSTOMER_ENTRY_LOAD](
[CARD_NUMBER] [varchar](12) NULL,
[TITLE] [varchar](6) NULL,
[LAST_NAME] [varchar](34) NULL,
[FIRST_NAME] [varchar](40) NULL,
[MIDDLE_NAME] [varchar](40) NULL,
[NAME_ON_CARD] [varchar](26) NULL,
[H_ADDRESS_PREFIX] [varchar](50) NULL,
[H_FLAT_NUMBER] [varchar](5) NULL,
[H_STREET_NUMBER] [varchar](10) NULL,
[H_STREET_NUMBER_SUFFIX] [varchar](5) NULL,
[H_STREET] [varchar](50) NULL,
[H_SUBURB] [varchar](50) NULL,
[H_CITY] [varchar](50) NULL,
[H_POSTCODE] [varchar](4) NULL,
[P_ADDRESS_PREFIX] [varchar](50) NULL,
[P_FLAT_NUMBER] [varchar](5) NULL,
[P_STREET_NUMBER] [varchar](10) NULL,
[P_STREET_NUMBER_SUFFIX] [varchar](5) NULL,
[P_STREET] [varchar](50) NULL,
[P_SUBURB] [varchar](50) NULL,
[P_CITY] [varchar](50) NULL,
[P_POSTCODE] [varchar](4) NULL,
[H_STD] [varchar](3) NULL,
[H_PHONE] [varchar](7) NULL,
[C_STD] [varchar](3) NULL,
[C_PHONE] [varchar](10) NULL,
[W_STD] [varchar](3) NULL,
[W_PHONE] [varchar](7) NULL,
[W_EXTN] [varchar](5) NULL,
[DOB] [smalldatetime] NULL,
[EMAIL] [varchar](50) NULL,
[DNS_STATUS] [bit] NULL,
[DNS_EMAIL] [bit] NULL,
[CREDITCARD] [char](1) NULL,
[PRIMVISACUSTID] [int] NULL,
[PREFERREDNAME] [varchar](100) NULL,
[STAFF_NUMBER] [varchar](50) NULL,
[CUSTOMER_ID] [int] NULL,
[IS_ADDRESS_VALIDATED] [varchar](50) NULL
) ON [PRIMARY]
BULK INSERT STATEMENT:
SET #string_temp = 'BULK INSERT customer_entry_load FROM '+char(39)+#inpath
+#current_file+'.txt'+char(39)+' WITH (FIELDTERMINATOR = '+char(39)+'|'+char(39)
+', MAXERRORS=1000, ROWTERMINATOR = '+char(39)+'\n'+char(39)+')'
SET DATEFORMAT dmy
EXEC(#string_temp)
The documentation describes how to use a format file to handle the scenario where the target table has more columns than the source file. An alternative that can sometimes be easier is to create a view on the table and BULK INSERT into the view instead of the table; this possibility is described in the same documentation.
And please always mention your SQL Server version.
Using OPENROWSET with BULK allows you to use your file in a query. You can use that to format the data and select only the columns you need.
In the end I have handled the two different cases with two different BULK INSERT statements (depending on which file is being processed). It seems like there isn't a way to do what I was trying to do with one statement.
You could use the format file idea supplied by #Pondlife.
Adapt your insert dynamically based on the input file name (provided there are unique differneces between the external parties). Using a CASE statement, simply select the correct format file based on the unique identifier in the file name.
DECLARE #formatFile varchar (max);
Set #formatFile =
CASE
WHEN #current_file LIKE '%uniqueIdentifier%'
THEN 'file1'
ELSE 'file2'
END
SET #string_temp = 'BULK INSERT customer_entry_load FROM '+char(39)+#inpath
+#current_file+'.txt'+char(39)+' WITH (FORMATFILE = '+char(39)+#formatFile+char(39)
')'
SET DATEFORMAT dmy
EXEC(#string_temp)
Hope that helps!

Resources