Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Best Way to Move Data

Status
Not open for further replies.

ksaab2002

MIS
Jan 6, 2005
66
US
Hello,

I would like to make a structural copy of an existing table and populate it with data from another table - is there an easy way to do this?

The tables are identical except that one allows nulls and another does not -- I am trying to in essence get rid of the allow nulls and the <NULL> values...

Thanks!
 
Try this.
Code:
select *
into {NewTable}
from {OldTable}

Denny
MCSA (2003) / MCDBA (SQL 2000)

--Anything is possible. All it takes is a little research. (Me)

[noevil]
(Not quite so old any more.)
 
Will that get rid of the <NULL> values in my existing table? (If the new table does not have "Allow Nulls" checked) or will it bring in all the <NULL> values?

 

Hi in this code I have used

ExistingTable = your source table
NewTable = new table with the structure of existingtable
OtherTable = Table from which you want to get the data

Code:
/* Creating new table */
declare @name varchar(255) , @DtType varchar(100), @Length int, @SQLStr nvarchar(4000)

Declare curFieldNames cursor for 
Select B.[name] as FieldName, type_name(B.xusertype) as DataType , convert(int, B.length) as Length from SysObjects A , SysColumns B where A.id = b.id and A.Name = '<ExistingTable>' Order By colid

set @sqlstr  = ''
open curFieldNames
fetch next from curFieldNames into @Name, @DTType, @Length
while @@Fetch_Status = 0
begin

if len(ltrim(rtrim(@SQLSTR))) > 0
	set @SQLSTR = @SQLSTR + ', '

if @DTType = 'datetime' or @Dttype = 'int'
begin
	set @SQLStr = @SQLStr + @Name + ' ' + @DTType 
end
else
begin
	set @SQLStr = @SQLStr + @Name + ' ' + @DTType + ' (' + convert( varchar, @length) + ')'
end
fetch next from curFieldNames into @Name, @DTType, @Length
end

Close curFieldNames
deallocate curFieldNames

set @SQLStr = 'Create table NewTable (' + @SQLStr + ')'
print @SQLSTR
execute (@SQLSTR)

/* inserting values from  other table which is identical */
insert into newTable select * from othertable

hope this will fulfill your requirement.



Regards,


"There are no secrets to success. It is the result of preparation, hard work, and learning from failure." -- Colin Powell
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top