?
Solved

insert sql syntax

Posted on 2005-05-03
10
Medium Priority
?
225 Views
Last Modified: 2010-03-19
Hi:
  I get the following error when I try something like:
insert into dbX.dbo.A select * from  dbX.dbo.B

String or binary data would be truncated.
The statement has been terminated.

The schema of both the tables are the same and look like:
a datetime,
b varchar(30),
c char(5),
d varchar(4),
e int,
f int,
g int,
h int,
i int,
j money,
k money,
l money,
m money)

Does anyone know why this error should come when the schemas are the same (so no fear of chop)? how can i work around this if not?

thanks
0
Comment
Question by:LuckyLucks
10 Comments
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13921108
The schema of both tables may be the same, but make sure that the sequence of the columns in both tables are in the same order.  If the sequence of the columns are not the same, then you have to provide the column names in the INSERT statement as well as in the SELECT statement to make sure they match.
0
 
LVL 14

Assisted Solution

by:adwiseman
adwiseman earned 900 total points
ID: 13921139
If this where me, I'd double check that both tables are the same.  Are these both SQL server tables?  Are these all the fields and datatypes in the tables?  Obviously you've used an example, any text, nvarchar, binary, Primary Keys, etc?

Then if I'm still getting the error, I'd explicitly write out the columns in the insert, then I can start taking them away untill the error goes away.  You may find the column that is causing the error.

insert into dbX.dbo.A(a, b, c, ...) select (a, b, c, ...) from  dbX.dbo.B


If the tables are identical, then you should not have an issue.  There's something that's not the same.
0
 
LVL 13

Expert Comment

by:Torrwin
ID: 13921157
If that still doesn't work you could just export the data from table B to table A in enterprise manager.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:LuckyLucks
ID: 13921747
the sequence is the same and so are the field names. The field that seems to be giving the prolem is of type varchar(4). It writes into the initial table B as lastname,firstname s.t the longest field has 20 chars. But when it tries the insert into A , it cant copy over the same field even tho they both are 4 varchars. How can I resolve this?

thanks
0
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13921795
In your INSERT ... SELECT statement, specify the column names.  Then in the column that has only 4 characters, use LEFT(ColumnName, 4) so that it will only insert 4 characters.
0
 

Author Comment

by:LuckyLucks
ID: 13927438
still doesnt. I was wondering if there was a method tosplit a field on a character say lname, fname could be split into lname and fname based on the comma?
0
 
LVL 28

Accepted Solution

by:
rafrancisco earned 600 total points
ID: 13927519
>> I was wondering if there was a method tosplit a field on a character say lname, fname could be split into lname and fname based on the comma? <<

DECLARE @LName VARCHAR(100)
DECLARE @FName VARCHAR(100)
DECLARE @FullName VARCHAR(200)

SET @FullName = 'Lucks,Lucky'
SET @LName = LEFT(@FullName, CHARINDEX(',', @FullName) - 1)
SET @FName = RIGHT(@FullName, CHARINDEX(',', REVERSE(@FullName)) - 1)
0
 

Author Comment

by:LuckyLucks
ID: 13927778
     
ALso to note , I created the lname, fname monster from below in an sql stmt:
ltrim(rtrim(max(x.dbm_last_name)))+', '+ltrim(rtrim(max(x.dbm_first_name))) as "FullName"

and now when I do   ltrim(rtrim(FullName)) I get the entire lname, fname.
0
 
LVL 28

Expert Comment

by:rafrancisco
ID: 13928440
>> and now when I do   ltrim(rtrim(FullName)) I get the entire lname, fname. << 

What do you mean by this?

To help you better, why not post the table definition of both your tables as well as the SQL statement you are trying to execute.
0
 

Author Comment

by:LuckyLucks
ID: 13929059
coming soon....
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question