Friday, February 24, 2012
Appending two tables
each table is exactly the same, except they are for different years (one is
history and one is current year). I need to be able to create a view that has
both the history data and current year data so I can list transactions
chronologically from the two tables.Create View MyView
AS
Select Field1, Field2, Field3
From Table1
UNION
Select Field1, Field2, Field3
From Table2
"Ranjit Charles" <RanjitCharles@.discussions.microsoft.com> a écrit dans le
message de news: 055547F7-9031-4E92-9868-8A11493C7865@.microsoft.com...
>I have 2 SQL tables that Ineed to combine (append). The column information
>in
> each table is exactly the same, except they are for different years (one
> is
> history and one is current year). I need to be able to create a view that
> has
> both the history data and current year data so I can list transactions
> chronologically from the two tables.
Appending two tables
each table is exactly the same, except they are for different years (one is
history and one is current year). I need to be able to create a view that has
both the history data and current year data so I can list transactions
chronologically from the two tables.
Create View MyView
AS
Select Field1, Field2, Field3
From Table1
UNION
Select Field1, Field2, Field3
From Table2
"Ranjit Charles" <RanjitCharles@.discussions.microsoft.com> a crit dans le
message de news: 055547F7-9031-4E92-9868-8A11493C7865@.microsoft.com...
>I have 2 SQL tables that Ineed to combine (append). The column information
>in
> each table is exactly the same, except they are for different years (one
> is
> history and one is current year). I need to be able to create a view that
> has
> both the history data and current year data so I can list transactions
> chronologically from the two tables.
Appending two tables
n
each table is exactly the same, except they are for different years (one is
history and one is current year). I need to be able to create a view that ha
s
both the history data and current year data so I can list transactions
chronologically from the two tables.Create View MyView
AS
Select Field1, Field2, Field3
From Table1
UNION
Select Field1, Field2, Field3
From Table2
"Ranjit Charles" <RanjitCharles@.discussions.microsoft.com> a crit dans le
message de news: 055547F7-9031-4E92-9868-8A11493C7865@.microsoft.com...
>I have 2 SQL tables that Ineed to combine (append). The column information
>in
> each table is exactly the same, except they are for different years (one
> is
> history and one is current year). I need to be able to create a view that
> has
> both the history data and current year data so I can list transactions
> chronologically from the two tables.
appending two datetimes which were converted from strings
the first is a date and the 2nd one is a time.
the time is converted from string.
thanksthe time is converted from string.
One method is to build a string containing both date and time in format is
'yyyymmdd hh:mm:ss'. SQL Server can then parse the string into a datetime
value. For example:
DECLARE
@.MyDate datetime,
@.MyTime datetime,
@.MyDateTime datetime
SET @.MyDate = '20061121'
SET @.MyTime = '12:13:14'
--concatenate date and time strings and assign to datetime data type
SET @.MyDateTime =
CONVERT(char(9), @.MyDate, 112) +
CONVERT(char(8), @.MyTime, 114)
--
Hope this helps.
Dan Guzman
SQL Server MVP
"paul_zaoldyeck" <niopaul@.yahoo.comwrote in message
news:1164084180.584403.64710@.k70g2000cwa.googlegro ups.com...
Quote:
Originally Posted by
can anyone teach me how to append to datetime data into one.
the first is a date and the 2nd one is a time.
>
the time is converted from string.
>
thanks
>
i have another problem.
how can i convert a char(4) into a time where the datatype of the
column is datatime?
i think this is my only problem now.thanks
Dan Guzman wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
the time is converted from string.
>
One method is to build a string containing both date and time in format is
'yyyymmdd hh:mm:ss'. SQL Server can then parse the string into a datetime
value. For example:
>
DECLARE
@.MyDate datetime,
@.MyTime datetime,
@.MyDateTime datetime
>
SET @.MyDate = '20061121'
SET @.MyTime = '12:13:14'
>
--concatenate date and time strings and assign to datetime data type
SET @.MyDateTime =
CONVERT(char(9), @.MyDate, 112) +
CONVERT(char(8), @.MyTime, 114)
>
--
Hope this helps.
>
Dan Guzman
SQL Server MVP
>
"paul_zaoldyeck" <niopaul@.yahoo.comwrote in message
news:1164084180.584403.64710@.k70g2000cwa.googlegro ups.com...
Quote:
Originally Posted by
can anyone teach me how to append to datetime data into one.
the first is a date and the 2nd one is a time.
the time is converted from string.
thanks
Quote:
Originally Posted by
how can i convert a char(4) into a time where the datatype of the
column is datatime?
Combine it with an appropriate date (today's date, a date from a
related record, whatever) in a similar fashion to the previous
answer. If you don't care about the date, then pick a fixed value
(e.g. January 1, 1970) and use that in all cases.|||The example below parses a string in hh:mm format into a datetime variable.
Note that a datetime always includes both date and time components so your
app will need to ignore the date part, if not needed. The date defaults to
'19000101'.
DECLARE
@.MyTimeString char(4),
@.MyDateTime datetime
SET @.MyTimeString = '1213' --hh:mm
SET @.MyDateTime = LEFT(@.MyTimeString, 2) + ':' + LEFT(@.MyTimeString, 2) +
':00'
SELECT @.MyDateTime
--
Hope this helps.
Dan Guzman
SQL Server MVP
"paul_zaoldyeck" <niopaul@.yahoo.comwrote in message
news:1164254851.864579.14760@.b28g2000cwb.googlegro ups.com...
Quote:
Originally Posted by
thanks for the answer.it worked.
>
i have another problem.
>
how can i convert a char(4) into a time where the datatype of the
column is datatime?
>
i think this is my only problem now.thanks
Dan Guzman wrote:
Quote:
Originally Posted by
Quote:
Originally Posted by
the time is converted from string.
>>
>One method is to build a string containing both date and time in format
>is
>'yyyymmdd hh:mm:ss'. SQL Server can then parse the string into a
>datetime
>value. For example:
>>
>DECLARE
> @.MyDate datetime,
> @.MyTime datetime,
> @.MyDateTime datetime
>>
>SET @.MyDate = '20061121'
>SET @.MyTime = '12:13:14'
>>
>--concatenate date and time strings and assign to datetime data type
>SET @.MyDateTime =
> CONVERT(char(9), @.MyDate, 112) +
> CONVERT(char(8), @.MyTime, 114)
>>
>--
>Hope this helps.
>>
>Dan Guzman
>SQL Server MVP
>>
>"paul_zaoldyeck" <niopaul@.yahoo.comwrote in message
>news:1164084180.584403.64710@.k70g2000cwa.googlegro ups.com...
Quote:
Originally Posted by
can anyone teach me how to append to datetime data into one.
the first is a date and the 2nd one is a time.
>
the time is converted from string.
>
thanks
>
>
appending to the end of current file
ThanksDarren Green suggested in earlier posts that :
The text file provider does not support this. The simplest thing to do is to export to a new file, then use the DOS copy command to merge the files.
This can be done with the Execute Process Task, and any dynamic filename stuff can be handled with an ActiveX Script Task to change the connection and the Exec Proc Task at the same time.
copy FileOriginal.txt+FileNew.txt FileResult.txt
HTH|||That is what I am currently doing. I was hoping for something within the dts package. Thanks for you help.
val|||As suggested its not available using TEXT provider with DTS.
May be this is also other way around, is to export to an excel sheet and save that as text file.:)
Appending to a text file via DTS
Kindly let me know where I am going wrong.I am having the same issue as well... I thought I remember seeing an option checkbox somewhere to do that... but I cant seem to find it.
I doubt we are the only ones having to deal with this, so can someone please chime in with an answer or maybe a possible direction for us to follow? Thanks in advance... :)
Appending to a text field using UPDATE
UPDATE wr SET cf_notes = " + tmp_array(24) + " WHERE wr_id = " + data_temp(0)
I would like to add cf_notes + tmp_array(24) to cf_notes. Is this possible in SQL? If so, what is the correct syntax. I have tried 6 different statements and I get a compile error on every statement.
Thanks,
SBRcreate procedure spU_update_table_with_text (
@.key_field_value int,
@.text text = null)
as
declare @.txtptr binary(16), @.insert_offset int
select @.txtptr = textptr(text_field),
@.insert_offset = datalength(text_field) + 1 --or 2 if a space is needed
from your_table
where key_field = @.key_field_value
updatetext your_table.text_field @.insert_offset 0 @.text
return
Appending to a Text field
I am trying to append to a text (text data type) field in my database.
Simply put, the Customer table has a field called Notes. I want to append
some extra data to this field via TSQL. So for a Customer who has an ID of
1234, how do I append some extra text to his Notes field? The help in Books
on Line has me completely baffled.
Thank youWhich helo are you referring to? Have you looked at WRITETEXT/UPDATETEXT?
ML
http://milambda.blogspot.com/
Appending to a recordset destination
We have a data-flow that extracts from a raw file and inserts into a datareader destination.
The data-flow requires DelayValidation=TRUE because of the raw file source (http://blogs.conchango.com/jamiethomson/archive/2005/12/01/2443.aspx)
Unfortunately it also requires DelayValidation=FALSE because of the Datareader destination (http://blogs.conchango.com/jamiethomson/archive/2005/10/20/2284.aspx)
So basically we can't do it this way.
We'd like to replace the raw file destination with a recordset destination but we have to be able to append to it.
Is there a way of appending when using a recordset destination?
Thanks in advance!
-JamieJamie,
I believe that your first statement is not correct for the raw file source (just for the raw file destination) as long as you have already created the raw file before the dataflow with the raw file source validates. So you could create a empty raw file with just the correct metadata and then set delay validation to false and what you want to do should work.
In answer to your question, I do not believe we have a way to append using a datareader dest.
Thanks,
Matt|||Matt,
That's true - but I don't really want to be moving raw files into a production envioronment. We want to deploy the packages and nothing else!
We are thinking of writing a custom destination adapter that appends to a recordset. Do you think that is possible?
I guess its a question of which is the least nasty workaround so your suggestion IS something to consider.
-Jamie
P.S. Is it worth raising a DCR asking for the ability to append to a recordset destination? This would mean it could be used in place of a raw file which could be rather useful.|||Hi Jamie,
I am certain it is possible to create a custom destination adapter to do what you want. I don't know that it wouldn't be really nasty to do so. I think the empty raw file is a little more hacky but much easier to do.
I can't see why you couldn't raise this as a DCR. I don't know whether it would make it into the next version or not but that isn't reason not to ask.
Thanks,
Matt|||OK. Well I have done. :)
http://lab.msdn.microsoft.com/ProductFeedback/thankyou.aspx?FeedbackID=FDBK42005&submit=true
-Jamie
Appending to a record
I have created a Table that contains a WorksheetID field and a SalesNotes field. I can successfully populate that table but I am uncertain as to how I can append to the SalesNotes record.
I want to keep the orginal notes but populate addtional notes.
Would I do this by using the UPDATE call?
Many Thanks
T
Hi,you explanation is a bit unclear, but if you want to fill the table with additional data which can′t be filled during the initial load, you can sure use an update statement to modify the exisiting records.
HTH, Jens Suessmeyer.http://www.sqlserver2005.de
Appending to a record
I have created a Table that contains a WorksheetID field and a SalesNotes field. I can successfully populate that table but I am uncertain as to how I can append to the SalesNotes record.
I want to keep the orginal notes but populate addtional notes.
Would I do this by using the UPDATE call?
Many Thanks
T
Hi,you explanation is a bit unclear, but if you want to fill the table with additional data which can′t be filled during the initial load, you can sure use an update statement to modify the exisiting records.
HTH, Jens Suessmeyer.http://www.sqlserver2005.de
Appending Text to a SQL Text Data Type
m
the selections from a series of VarChar(8000) values. I know I need to use
UpdateText, but it only seems to store the 1st one it reads. Can anyone hel
p?
Here's my text:
Declare @.TriggerText nVarChar(4000)
Declare @.ptrval Binary(16)
Declare @.Offset Int
-- Create temporary table to hold Text field
Create Table #tempTrigger
(TextField Text NULL)
Insert Into #tempTrigger Select ''
-- Get Trigger "basis"
Declare curTriggerBasis Insensitive Cursor For
Select c.Text
From sysObjects o (nolock)
Inner Join sysComments c (nolock)
On o.ID = c.ID
Where o.Name = 'cttx_Customer'
Order By ColID
For Read Only
Open curTriggerBasis
Fetch Next From curTriggerBasis Into @.TriggerText
While @.@.Fetch_Status = 0
Begin
Select @.ptrval = TEXTPTR(TextField),
@.Offset = DataLength(TextField)
From #tempTrigger (nolock)
UpdateText #tempTrigger.TextField @.ptrval @.Offset 0 @.TriggerText
Fetch Next From curTriggerBasis Into @.TriggerText
End
Close curTriggerBasis
Deallocate curTriggerBasis
Select * From #tempTrigger (nolock)> but it only seems to store the 1st one it reads
How are you determining this? What does 'SELECT DATALENGTH(TextField) FROM
#tempTrigger' return?
Happy Holidays
Dan Guzman
SQL Server MVP
"bobnunny" <u17151@.uwe> wrote in message news:59ac1c8e96b9c@.uwe...
>I am trying to use a cursor to create a mass Text field with the results
>from
> the selections from a series of VarChar(8000) values. I know I need to
> use
> UpdateText, but it only seems to store the 1st one it reads. Can anyone
> help?
> Here's my text:
> Declare @.TriggerText nVarChar(4000)
> Declare @.ptrval Binary(16)
> Declare @.Offset Int
> -- Create temporary table to hold Text field
> Create Table #tempTrigger
> (TextField Text NULL)
> Insert Into #tempTrigger Select ''
> -- Get Trigger "basis"
> Declare curTriggerBasis Insensitive Cursor For
> Select c.Text
> From sysObjects o (nolock)
> Inner Join sysComments c (nolock)
> On o.ID = c.ID
> Where o.Name = 'cttx_Customer'
> Order By ColID
> For Read Only
> Open curTriggerBasis
> Fetch Next From curTriggerBasis Into @.TriggerText
> While @.@.Fetch_Status = 0
> Begin
> Select @.ptrval = TEXTPTR(TextField),
> @.Offset = DataLength(TextField)
> From #tempTrigger (nolock)
> UpdateText #tempTrigger.TextField @.ptrval @.Offset 0 @.TriggerText
> Fetch Next From curTriggerBasis Into @.TriggerText
> End
> Close curTriggerBasis
> Deallocate curTriggerBasis
> Select * From #tempTrigger (nolock)|||I've put Print statements in there to check this out. It shows Datalength a
s
4000 everytime except the last one. BUT, like an idiot I was checking the
loop so hard, but the Select statement at the end will only return the first
4000. Once I changed that to DataLength, it showed it had it all.
Thanx!
Dan Guzman wrote:
>How are you determining this? What does 'SELECT DATALENGTH(TextField) FRO
M
>#tempTrigger' return?
>
>[quoted text clipped - 36 lines]
appending tables
structure of both tables is precisely the same? Also, can this be set up to
append only those that meet a certain criteria, such that a 'where' clause
could be used for the append?
Thanks for any help.
Bernie YaegerINSERT INTO TABLE1
SELECT column_list FROM TABLE2
WHERE <condition in TABLE2>
--
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Bernie Yaeger" <berniey@.cherwellinc.com> wrote in message
news:0Noeb.22436$Kq4.15794796@.news4.srv.hcvlny.cv.net...
> Using t-sql, what's the easiest way to append one table to another where
the
> structure of both tables is precisely the same? Also, can this be set up
to
> append only those that meet a certain criteria, such that a 'where' clause
> could be used for the append?
> Thanks for any help.
> Bernie Yaeger
>|||Hi Kalen,
Tx so much!
Regards,
Bernie
"Kalen Delaney" <replies@.public_newsgroups.com> wrote in message
news:e6%23nkJ7hDHA.3208@.TK2MSFTNGP11.phx.gbl...
> INSERT INTO TABLE1
> SELECT column_list FROM TABLE2
> WHERE <condition in TABLE2>
> --
> HTH
> --
> Kalen Delaney
> SQL Server MVP
> www.SolidQualityLearning.com
>
> "Bernie Yaeger" <berniey@.cherwellinc.com> wrote in message
> news:0Noeb.22436$Kq4.15794796@.news4.srv.hcvlny.cv.net...
> > Using t-sql, what's the easiest way to append one table to another where
> the
> > structure of both tables is precisely the same? Also, can this be set
up
> to
> > append only those that meet a certain criteria, such that a 'where'
clause
> > could be used for the append?
> >
> > Thanks for any help.
> >
> > Bernie Yaeger
> >
> >
>
Appending tables
I wanted to know if it is possible to do to append two tables into a
third table.
For example, consider these two tables
Table 1
-------------------
| Part_num | Prt_name | Desc1 | Desc2 |
-------------------
| PRT1 | PartA | abc | xyz |
| PRT2 | PartB | def | aaa |
| PRT3 | PartC | ghi | bbb |
-------------------
Table 2
-------------------
| Cat_num | Cat_name | SDsc1 | SDsc2 |
-------------------
| CAT1 | CatalogA | abc | xyz |
| CAT2 | CatalogB | def | aaa |
| CAT3 | CatalogC | ghi | bbb |
-------------------
Now, I want to append them to get this :
Table 3
--------------------------------------
| Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
SDsc1 | SDsc2 |
--------------------------------------
| PRT1 | PartA | abc | xyz |
|
| PRT2 | PartB | def | aaa |
|
| PRT3 | PartC | ghi | bbb |
|
| | | | | CAT1
| CatalogA | abc | xyz |
| | | | | CAT2
| CatalogB | def | aaa |
| | | | | CAT3
| CatalogC | ghi | bbb |
--------------------------------------
The blanks in Table 3 are , well ,blank.
Now can it be done or not?
Awaiting your replies,
Regards,
ShwetabhShwetabh wrote:
> Hi,
> I wanted to know if it is possible to do to append two tables into a
> third table.
> For example, consider these two tables
> Table 1
> -------------------
> | Part_num | Prt_name | Desc1 | Desc2 |
> -------------------
> | PRT1 | PartA | abc | xyz |
> | PRT2 | PartB | def | aaa |
> | PRT3 | PartC | ghi | bbb |
> -------------------
> Table 2
> -------------------
> | Cat_num | Cat_name | SDsc1 | SDsc2 |
> -------------------
> | CAT1 | CatalogA | abc | xyz |
> | CAT2 | CatalogB | def | aaa |
> | CAT3 | CatalogC | ghi | bbb |
> -------------------
>
> Now, I want to append them to get this :
>
> Table 3
> --------------------------------------
> | Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
> SDsc1 | SDsc2 |
> --------------------------------------
> | PRT1 | PartA | abc | xyz |
> |
> | PRT2 | PartB | def | aaa |
> |
> | PRT3 | PartC | ghi | bbb |
> |
> | | | | | CAT1
> | CatalogA | abc | xyz |
> | | | | | CAT2
> | CatalogB | def | aaa |
> | | | | | CAT3
> | CatalogC | ghi | bbb |
> --------------------------------------
>
> The blanks in Table 3 are , well ,blank.
> Now can it be done or not?
> Awaiting your replies,
> Regards,
> Shwetabh
My browser isn't displaying your Table 3 very well so I'm not quite
certain which data is going into which column. In general you can merge
tables like this using UNION:
SELECT part_num, prt_name, desc1, ...
FROM Table1
UNION ALL
SELECT NULL, NULL, cat_name, ...
FROM Table2 ;
Each SELECT list in the UNION has to have the same number of columns
and each column has to be made up of compatible datatypes. In your case
it seems like the big question is what will be the key of Table3? It
isn't clear to me whether it has a key at all.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Shwetabh (shwetabhgoel@.gmail.com) writes:
> Table 3
> -----------------------
----------------
>| Part_num | Prt_name | Desc1 | Desc2 | Cat_num | Cat_name |
> SDsc1 | SDsc2 |
> -----------------------
----------------
>| PRT1 | PartA | abc | xyz |
> |
>| PRT2 | PartB | def | aaa |
> |
>| PRT3 | PartC | ghi | bbb |
> |
>| | | | | CAT1
> | CatalogA | abc | xyz |
>| | | | | CAT2
> | CatalogB | def | aaa |
>| | | | | CAT3
> | CatalogC | ghi | bbb |
> -----------------------
----------------
>
> The blanks in Table 3 are , well ,blank.
> Now can it be done or not?
Judging from the sample data you posted, what you want is
SELECT a.Part_num, a.Prt_name, a.Desc1, a.Desc2, b.Cat_num,
b.Cat_name, b.SDsc1, b.SDcs2
FROM table1 a
JOIN table2 b ON a.Desc1 = b.SDsc1
AND b.Desc2 = b.SDcs2
But I cannot say that it make much sense to join over a description column.
Maybe you need to consider a little more what you are actually looking
for and what you want to achieve.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Hi,
it seems Table 3 got pretty messed up.
So I will give the schema definations of the tables here:
Table 1:
CREATE TABLE TABLE1
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20)
)
Table 2:
CREATE TABLE TABLE2
(
PART_NUM varchar(10) primary key,
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)
Now the resultant table should have the following schema:
CREATE TABLE TABLE3
(
PART_NUM varchar(10) primary key,
PRT_NAME VARCHAR(10),
DESC1 VARCHAR(20),
DESC2 VARCHAR(20),
CAT_NUM VARCHAR(10),
CAT_NAME VARCHAR(10),
SDESC1 VARCHAR(20),
SDESC2 VARCHAR(20)
)
This schema will be created programmatically.
Now my question is, if it is possible, how can I
insert records from table1 and table2 in table3?
I hope I have now made the things clearer.
Awaiting your reply,
Regards,
Shwetabh|||Shwetabh wrote:
> Hi,
> it seems Table 3 got pretty messed up.
> So I will give the schema definations of the tables here:
> Table 1:
> CREATE TABLE TABLE1
> (
> PART_NUM varchar(10) primary key,
> PRT_NAME VARCHAR(10),
> DESC1 VARCHAR(20),
> DESC2 VARCHAR(20)
> )
> Table 2:
> CREATE TABLE TABLE2
> (
> PART_NUM varchar(10) primary key,
> CAT_NUM VARCHAR(10),
> CAT_NAME VARCHAR(10),
> SDESC1 VARCHAR(20),
> SDESC2 VARCHAR(20)
> )
> Now the resultant table should have the following schema:
> CREATE TABLE TABLE3
> (
> PART_NUM varchar(10) primary key,
> PRT_NAME VARCHAR(10),
> DESC1 VARCHAR(20),
> DESC2 VARCHAR(20),
> CAT_NUM VARCHAR(10),
> CAT_NAME VARCHAR(10),
> SDESC1 VARCHAR(20),
> SDESC2 VARCHAR(20)
> )
> This schema will be created programmatically.
> Now my question is, if it is possible, how can I
> insert records from table1 and table2 in table3?
> I hope I have now made the things clearer.
> Awaiting your reply,
> Regards,
> Shwetabh
It looks like you'll want something like this:
INSERT INTO Table3
(part_num, prt_name, desc1, desc2,
cat_num, cat_name, sdesc1, sdesc2)
SELECT COALESCE(T1.part_num, T2.part_num),
T1.prt_name, T1.desc1, T1.desc2,
T2.cat_num, T2.cat_name, T2.sdesc1, T2.sdesc2
FROM Table1 AS T1
FULL JOIN Table2 AS T2
ON T1.part_num = T2.part_num ;
It still seems at least questionable whether Table3 or even Table2
represent "good" designs but as I only have your column names to go on
there isn't much point in me speculating about that.
--
David Portas, SQL Server MVP
Whenever possible please post enough code to reproduce your problem.
Including CREATE TABLE and INSERT statements usually helps.
State what version of SQL Server you are using and specify the content
of any error messages.
SQL Server Books Online:
http://msdn2.microsoft.com/library/...US,SQL.90).aspx
--|||Hi,
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.
As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.
Also, till now, I have converted DBASE database into SQL database using
OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.
Any ideas?|||Hi,
Well I cant really do anything about the design for table2 because that
is the way the client wants it to be.
As far as table3 goes, I wanted to check the possiblility of appending
the
records of all the tables into a single table and check the performance
and
efficiency. I understand that table3 is a poor database design
but the main motivation for doing this is to check the reaction of the
application
which will using such database.
Also, till now, I have converted DBASE database into SQL database using
OPENROWSET to import the data, I was wondering if the same database
can be entered into a new table which can hold the data from all
tables.
Any ideas?
Regards,
Shwetabh
P.S: If you want I can mail you the code I have written in VB for this
purpose.|||In table one, there are rows or records. Take the "first one."
Now in table two there are a bunch of rows or records.
how do we know which record or records from the second to table to
combine with the first row of the first table?
In other words, if there are 100 rows in the first table, and 10 rows
in the second, how many rows are you expecting in the third table?|||Shwetabh (shwetabhgoel@.gmail.com) writes:
> Well I cant really do anything about the design for table2 because that
> is the way the client wants it to be.
> As far as table3 goes, I wanted to check the possiblility of appending
> the
> records of all the tables into a single table and check the performance
> and
> efficiency. I understand that table3 is a poor database design
> but the main motivation for doing this is to check the reaction of the
> application
> which will using such database.
> Also, till now, I have converted DBASE database into SQL database using
> OPENROWSET to import the data, I was wondering if the same database
> can be entered into a new table which can hold the data from all
> tables.
> Any ideas?
For it to be meaningful to merge table1 and table2 into one table,
there must be some relation between the data. Is there any such relation?
In your sample data PRT1 went with CAT1, but you did not indicate what
rule said that these two should go together.
If you don't know what you want, we will not know either.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:
> Shwetabh (shwetabhgoel@.gmail.com) writes:
> > Well I cant really do anything about the design for table2 because that
> > is the way the client wants it to be.
> > As far as table3 goes, I wanted to check the possiblility of appending
> > the
> > records of all the tables into a single table and check the performance
> > and
> > efficiency. I understand that table3 is a poor database design
> > but the main motivation for doing this is to check the reaction of the
> > application
> > which will using such database.
> > Also, till now, I have converted DBASE database into SQL database using
> > OPENROWSET to import the data, I was wondering if the same database
> > can be entered into a new table which can hold the data from all
> > tables.
> > Any ideas?
> For it to be meaningful to merge table1 and table2 into one table,
> there must be some relation between the data. Is there any such relation?
> In your sample data PRT1 went with CAT1, but you did not indicate what
> rule said that these two should go together.
> If you don't know what you want, we will not know either.
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx
Hi,
Please disregard the sample data for now. I had missed a field in table
2 there.
Let's look at the schema I have given for table 1,2,3. Here, I am able
to create
table 3 programmatically and it successfully creates all the columns in
table 1
and table 2.
As for the rules, let me make it clearer. The application (EasyLabel
from www.tharo.com) will be using this database. This application is
used to create labels. Each label consists of various components like
barcodes,images etc. The data for this label is mapped to the fields in
the database. The user enters the Part_num (which is unique for all
records) and the software retrieves the fields *which* are mapped from
the database and loads them in the label. In case there are some other
fields which are not mapped to the database, the application does not
care about them. In other words, they are as good as blank.
Now, what I have in mind is to create a table such that all Database is
stored in it. Since
the application will load only those fields into the labels which are
mapped, it wouldnt create a problem.
Now if I have 100 records in table 1 and 10 records in table 2, the
table 3 should have 110 records at the end of operation. This table
alone will be accessed by the application to do its work.
My question is how to do this? I mean how can I take the records from
table 1, put them in table 3, then take the records from table 2 and
put them in table 3 , and so on?
Awaiting replies,
Regards,
Shwetabh|||I am expecting 110 rows.
All i want is that first all records from table 1 are added to table 3.
Then all records from table 2 are added to table 3.
The fields which are not present in table 1 or table 2 are left blank.
One more question, if I have a table in SQL,
can I alter a field to make it primary key?
Or do I have to to it while creating the table itself?|||> Now, what I have in mind is to create a table such that all Database is
> stored in it. Since
> the application will load only those fields into the labels which are
> mapped, it wouldnt create a problem.
> Now if I have 100 records in table 1 and 10 records in table 2, the
> table 3 should have 110 records at the end of operation. This table
> alone will be accessed by the application to do its work.
> My question is how to do this? I mean how can I take the records from
> table 1, put them in table 3, then take the records from table 2 and
> put them in table 3 , and so on?
To make a completely wild guess, this may be what you are looking for:
INSERT tbl3(PART_NUM, PART_NAME, DESC1, DESC2)
SELECT PART_NUM, PART_NAME, DESC1, DESC2
FROM tbl1
INSERT tbl3(PART_NUM, CAT_NUM, CAT_NAME, SDESC1, SDESC2)
SELECT PART_NUM, CAT_NUM, CAT_NAME, SDESC1, SDESC2
FROM tbl2 a
WHERE NOT EXISTS (SELECT *
FROM tbl3 WHERE a.PART_NUM = b.PART_NUM)
UPDATE tbl3
SET CAT_NUM = b.CAT_NUN,
CAT_NAME = b.CAT_NAME,
SDESC1 = b.SDESC1,
SDESC2 = b.SDESC2
FROM tbl3 a
JOIN tbl2 b ON a.PART_NUM = b.PART_NUM
>One more question, if I have a table in SQL,
>can I alter a field to make it primary key?
>Or do I have to to it while creating the table itself?
You cannot alter the field to make it a PK, but you can alter the table
to define a PK, if it does not have one. And PK can have more than one
column.
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx|||Erland Sommarskog wrote:
> > Now, what I have in mind is to create a table such that all Database is
> > stored in it. Since
> > the application will load only those fields into the labels which are
> > mapped, it wouldnt create a problem.
> > Now if I have 100 records in table 1 and 10 records in table 2, the
> > table 3 should have 110 records at the end of operation. This table
> > alone will be accessed by the application to do its work.
> > My question is how to do this? I mean how can I take the records from
> > table 1, put them in table 3, then take the records from table 2 and
> > put them in table 3 , and so on?
> To make a completely wild guess, this may be what you are looking for:
> INSERT tbl3(PART_NUM, PART_NAME, DESC1, DESC2)
> SELECT PART_NUM, PART_NAME, DESC1, DESC2
> FROM tbl1
> INSERT tbl3(PART_NUM, CAT_NUM, CAT_NAME, SDESC1, SDESC2)
> SELECT PART_NUM, CAT_NUM, CAT_NAME, SDESC1, SDESC2
> FROM tbl2 a
> WHERE NOT EXISTS (SELECT *
> FROM tbl3 WHERE a.PART_NUM = b.PART_NUM)
> UPDATE tbl3
> SET CAT_NUM = b.CAT_NUN,
> CAT_NAME = b.CAT_NAME,
> SDESC1 = b.SDESC1,
> SDESC2 = b.SDESC2
> FROM tbl3 a
> JOIN tbl2 b ON a.PART_NUM = b.PART_NUM
Thanks, but I found out another way to get the job done.
> >One more question, if I have a table in SQL,
> >can I alter a field to make it primary key?
> >Or do I have to to it while creating the table itself?
> You cannot alter the field to make it a PK, but you can alter the table
> to define a PK, if it does not have one. And PK can have more than one
> column.
Agreed, I can use
ALTER TABLE <tablename> ADD PRIMARY KEY (<fieldname>);
to alter the table and define a PK. But it works only if the
<fieldname> is
NOT NULL. Is there any way I can alter the table to make the field NOT
NULL?
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server 2005 at
> http://www.microsoft.com/technet/pr...oads/books.mspx
> Books Online for SQL Server 2000 at
> http://www.microsoft.com/sql/prodin...ions/books.mspx|||Shwetabh (shwetabhgoel@.gmail.com) writes:
> Is there any way I can alter the table to make the field NOT
> NULL?
ALTER TABLE ALTER COLUMN
--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pr...oads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodin...ions/books.mspx
Appending Stored Procedure
CREATE PROCEDURE [insert_GCEmployeeTerms_1]
(@.TM_#_1 [int],
@.FirstName_2 [nvarchar](50),
@.LastName_3 [nvarchar](50),
@.SocialSecurityNumber_4 [int],
@.DateHired_5 [datetime],
@.Status_6 [nvarchar](50),
@.TerminationDate_7 [datetime],
@.Title_8 [nvarchar](50),
@.DepartmentName_9 [nvarchar](50),
@.TermReason_10 [ntext],
@.VoluntaryInvoluntary_11 [nvarchar](50))
AS INSERT INTO [CommissionEmployee_Exclusionsdb].[dbo].[GCEmployeeTerms]
( [TM #],
[FirstName],
[LastName],
[SocialSecurityNumber],
[DateHired],
[Status],
[TerminationDate],
[Title],
[DepartmentName],
[TermReason],
[VoluntaryInvoluntary])
SELECT
( @.TM_#,
@.FirstName,
@.LastName,
@.SocialSecurityNumber,
@.DateHired,
@.Status,
@.TerminationDate,
@.Title,
@.DepartmentName,
@.TermReason,
@.VoluntaryInvoluntary)
FROM EmployeeGamingLicense
WHERE STATUS = 'TERMINATED'
GOMethinks thou wants not both tables and variables in yon SELECT list. There be dragons in that mix!
-PatP|||Thank You pat for that very old English/shakesperan/King Arthur answer..lol. Let me see if I interpreted that correctly...Your saying that my select list is incorrect, refering of both tables...I need to go look it up..sorry this is my first append stored procedure.|||If you stop and think about it, you are passing a whole flock (that would be a really technical term, eh?) of parameters to the procedure. This would be a good thing if you wanted to insert those parameters one time. You are also using a table with a where clause, which would be a good thing if you wanted to insert columns from that table for every row that qualified. However, doing both means that you want to insert one copy of the parameters that you are passing for every row that qualifies... Which is probably NOT what you intended!
You probably want to either use the table with column names, or the parameters without the table, but only one or the other!
-PatP|||I'm just being nosy now, but did you ever get this one sorted out to your satisfaction?
-PatP|||Sniff Sniff Sniff ...No :(
I just want to insert data from one table to another automatically through stored procedures. I can make part of the procedure (that Inserts), need to tell it to pull from another table...I dont know, keep getting errors its like a puzzle I can only get part of it but not all (Banging head against wall)|||a whole flock of parameters
Pat. I thought it was a bevy of parameters. Hmm. May need to add it to the lists...
http://encyclopedia.thefreedictionary.com/List%20of%20collective%20nouns%20by%20collective%2 0term%20A-K|||ok so your saying its just a bunch of parameters..a horde of parameters|||Sniff Sniff Sniff ...No :(Now, now M'Dear! You musn't get too upset. Might I suggest trying something like:CREATE PROCEDURE [insert_GCEmployeeTerms_1]
AS
INSERT INTO [CommissionEmployee_Exclusionsdb].[dbo].[GCEmployeeTerms] (
[TM #], [FirstName], [LastName]
, [SocialSecurityNumber], [DateHired], [Status]
, [TerminationDate], [Title], [DepartmentName]
, [TermReason], [VoluntaryInvoluntary]
) SELECT
a.TM_#, a.FirstName, a.LastName
, a.SocialSecurityNumber, a.DateHired, a.Status
, a.TerminationDate, a.Title, a.DepartmentName
, a.TermReason, a.VoluntaryInvoluntary)
FROM EmployeeGamingLicense AS a
WHERE a.STATUS = 'TERMINATED'
RETURN
GO-PatP|||I did what you said pat I got some Errors but what you put up gave me a great start I was not Referring to the Table.Fields that will do the inserting for me Fields in that Table are not exactly the same..(the column names) OK I GET IT A LIGHT HAS GONE OFF AND I TOTALLY GET IT NOW..Now I see why SQL was like WHAT THE HELL ARE YOU TALKING ABOUT..OMG I Die a Happy Woman now...Thank you so much Pat Your Truly a SQL GOD..I totally Get it now. :)
THANK YOU|||and Mccowley was correct I see his point now|||Now that you grok (http://searchsmallbizit.techtarget.com/sDefinition/0,,sid44_gci212216,00.html) how this works, you'll need to think a bit about how to limit the carnage. You won't always want to turn that procedure loose to copy every terminated employee row, or you'll end up with a gazillion copies of each row in GCEmployeeTerms (one for every time the procedure is run while that terminated row is present), which probably isn't what you want!
-PatP|||Ok...what happens is our dept gets a list of terminated employees, so when we get this list we go to the [GamingCommissiondb].[dbo].[EmployeeGamingLicense] which is the active table look up the employee and change their status to 'TERMINATED'. Now there can be as much as 20-30 terminations. The Stored Procedure will then take those employees that have been changed to Terminated and APPEND (Insert) them into the TERMINATION.tbl. One of the purposes of doing this is if the employee comes back as a rehire we can check to see if there was any negative action taken against their gaming license. Does that make sense??|||Oh heavens yes, but unless you delete the row from the active table when this happens, that row will be copied every time the procedure runs (at least as it is written right now). The only criteria that you are using to select rows is if they are flagged as terminated... You might want to be a wee bit more selective than that, maybe throwing in a test on the termination date or something wild and crazy like that!
-PatP|||I understand what your saying, I dont want my users to delete a record in error so I didnt want to delet the record from the active table through the stored procedure. I have a view that shows only the terminated employees from the active table that I look at everyone 2 weeks, just a QC thing. also both the tables have the TM# as a primary key, the first time I tried to execute the stored procedure I got "Pk constraint error duplicates"..something like that, so once I found out what records had already been appended to the TERMINATION table and which ones had not been the SP executed successfully. Does that make sense?|||Yep, that it does. You could also use a WHERE NOT EXISTS clause to test for the presence of the TM# while you were doing the INSERT operation. This might be simpler.
-PatP|||Yes your absolutely right, I'll alter the procedure then thank you again for your help Pat I sure do appreciate it :)
Appending Records to the Existing MS SQL EXPRESS SERVER Table.
Dear All,
I am Using MS SQL EXPRESS SERVER .I have installed all tools available to Express Edition site.
Now I have created my database on this .I have imported a table from my MS ACCESS database (Using ODBC Datasource).This table contains 10,000 records ,
Now I want to append 1 more access Table(5500 records) to the existing table having same fields.
How to do this.Can any body tell me?
Thanks and Regards
mukesh
Hi mukesh,
You can append the imported records into the existing table using SQL Server import & Export Wizard.
1.Select Your database from SQL Server Management Studio
2.Right Click on Database and go to the Task->Import Data menu Item
3. SQL Server Import & Export Wizard will be open., choose ur data source. as MicrosoftAccess, and select the MDB file
4.Press Next to Move onChoose a Destination Page, and select your Database Name from dropdown
5.Press Next to Move onSpecity Table Copy or QueryPage, and selectcopy data from one or more tables or viewradio button.
6. Press Next to Move onSelect Source Table and View , Select your Source Table , and Destination Table and then pressEdit button,Column Mappings dialog box will be open, chooseAppend rows to the destination tabelradion button option ( it will append the new records with existing records, in your case ur new 5500 records will be apended with existing 10,000 records )
7Press Next, and then Press Finish.Import Process will be started.
Thanks
Best Regards,
Muhammad AKhtar Shiekh
SQL Server Import & Export Wizard
|||Hi mukesh,
The way you imported the first table just load the second table but with a different name i.e Table2 to the same database and then you can use the query
Lets Table_1 is having 10000
Table_2 is having 5500
----------------
Insert into Table_1
Select * from Table_2
----------------
the simplest way to do the stuff...
Satya
Thanks Mr.Akhhttar
But sir in management studio I can't find "Import and Export option" there r these option,"detach","shrink","backup","restore"& generate scripts.
thanks and regards
mukesh
|||Thanks a lot,Mr.Satya
Sunday, February 19, 2012
Appending Records + Update
e
Directory. This table will be used in a training database. When employees
terminate, they disappear from AD. So I would like to run a query that woul
d
select all of today's employees, and selectively append to the Employees
table. New employees would be added. If the employee no longer exists in
the AD data source, I'd like to set a value from 1 to 0 in the Active column
,
indicating that the employee is no longer active but keeping the record for
historical searches. Any suggestions would be appreciated. Thanks, Pancho.Hi
Take a look at IF EXEISTS , or WHERE NOT / EXISTS clauses in the BOL to
compare Employees
"Pancho" <Pancho@.discussions.microsoft.com> wrote in message
news:1656A7F5-305D-420C-AE6F-3D6188C2A9FB@.microsoft.com...
> Hello, I have a need to refresh a table called Employees each day from
> Active
> Directory. This table will be used in a training database. When
> employees
> terminate, they disappear from AD. So I would like to run a query that
> would
> select all of today's employees, and selectively append to the Employees
> table. New employees would be added. If the employee no longer exists in
> the AD data source, I'd like to set a value from 1 to 0 in the Active
> column,
> indicating that the employee is no longer active but keeping the record
> for
> historical searches. Any suggestions would be appreciated. Thanks,
> Pancho.|||Sorry
Should be IF EXISTS
"Uri Dimant" <urid@.iscar.co.il> wrote in message
news:eM2JtevUGHA.5108@.tk2msftngp13.phx.gbl...
> Hi
> Take a look at IF EXEISTS , or WHERE NOT / EXISTS clauses in the BOL to
> compare Employees
>
> "Pancho" <Pancho@.discussions.microsoft.com> wrote in message
> news:1656A7F5-305D-420C-AE6F-3D6188C2A9FB@.microsoft.com...
>|||On Tue, 28 Mar 2006 08:50:01 -0800, Pancho wrote:
>Hello, I have a need to refresh a table called Employees each day from Acti
ve
>Directory. This table will be used in a training database. When employees
>terminate, they disappear from AD. So I would like to run a query that wou
ld
>select all of today's employees, and selectively append to the Employees
>table. New employees would be added. If the employee no longer exists in
>the AD data source, I'd like to set a value from 1 to 0 in the Active colum
n,
>indicating that the employee is no longer active but keeping the record for
>historical searches. Any suggestions would be appreciated. Thanks, Pancho.[/color
]
Hi Pancho,
UPDATE Employees
SET Active = 0
WHERE Active = 1
AND NOT EXISTS
(SELECT *
FROM AD
WHERE AD.KeyColumn = Employees.KeyColumn)
--
INSERT INTO Employees (KeyColumn, OtherColumn, Active)
SELECT KeyColumn, OtherColumn, 1
FROM AD
WHERE NOT EXISTS
(SELECT *
FROM Employees
WHERE AD.KeyColumn = Employees.KeyColumn)
Hugo Kornelis, SQL Server MVP
Appending one datatable to another ?
Is the merge method, what will work in this case ? I have two datatables with the exact same structure. How can I append the rows from table 2 onto the bottom of table 1 ? Is looping through the rows collection the only way ?
You can use UNION ALL to join two tables all records with same structrure( watch for the identity issue). Like
Select col1, col2 From table1
UNION ALL
Select col1, col2 From table2
|||
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
dt1.Merge(dt2);
This will append dt2 to dt1
Appending info into a column
ex. There are 3 tables where all of the information is the same except the last column. The last column contains different types.
The tables are formatted something like
Name|Phone|Coverage|AA
Name|Phone|Coverage|BB
Name|Phone|Coverage|CC
But I want to have one table that contains the info as
Name|Phone|Coverage|AA|BB|CC|
| = Column changeJoin (possibly outer join) the 3 tables on the common columns.|||Name|Phone|Coverage|AA|BB|CC|
But are you not going to have a record for each type here also?|||I am not even sure how to word this. I tried the join and made sure to connect all of the tables to the most important information. Then I only output the AA, BB, CC columns. So in essence I have 3 free standing columns that are based on the same ID.
Here comes the problem. How do I match these columns with their related row.
I would like to take these 3 columns and append them to the end of the row with the corresponding information.
ex. I have AA|BB|CC and have Name|Number|Date
and want to have Name|Number|Date|AA|BB|CC
(the original rows looked something like my original post description)
but not all rows that had a BB correspond to a row with AA and would I want them to end up looking like this
ex. Name|Number|Date| |BB|CC
I know this is confusing, but I think it is coherent.
Please help!
Originally posted by andrewst
Join (possibly outer join) the 3 tables on the common columns.
Appending from one column to another for every row in a table
I need to carry out a database cleaning/maintenance operation whereby I
take the text in one column called ShortDescription and add it to the
text in a column called AdditionalDetails.
I know how to copy from one to the other but that would have the effect
of overwriting the values in the AdditionalDetails column. I need
whatever is in the first column to be simply appended to whatever is in
the second. If the first has null in it, nothing should happen, and I
guess if the second has a null in it, the null should be replaced with
whatever's in the second column.
Can anyone advise me on how to do this? I'm sure it must be a relatively
simple operation but I've never seen it done.
Thanks to anyone who can help
SimonUpdate table1 set additionaldetails = isnull(additionaldetails,'') +
shortdescription
where shortdescription is not null
Jay
Simon Harvey wrote:
> Hi there,
> I need to carry out a database cleaning/maintenance operation whereby I
> take the text in one column called ShortDescription and add it to the
> text in a column called AdditionalDetails
> I know how to copy from one to the other but that would have the effect
> of overwriting the values in the AdditionalDetails column. I need
> whatever is in the first column to be simply appended to whatever is in
> the second. If the first has null in it, nothing should happen, and I
> guess if the second has a null in it, the null should be replaced with
> whatever's in the second column.
> Can anyone advise me on how to do this? I'm sure it must be a relatively
> simple operation but I've never seen it done.
> Thanks to anyone who can help
> Simon|||You need concatenation, which is implemented using the + character.
UPDATE
SET AdditionalDetails = AdditionalDetails + ShortDescription
WHERE ShortDescription IS NOT NULL
The problem with the query above is that if AdditionalDetails is NULL
the concatenation ends up as NULL. So the more robust version would
be:
UPDATE
SET AdditionalDetails = COALESCE(AdditionalDetails, '') +
ShortDescription
WHERE ShortDescription IS NOT NULL
Roy Harvey
Beacon Falls, CT
On Wed, 13 Dec 2006 16:20:40 +0000, Simon Harvey
<nothanks@.hotmail.com> wrote:
>Hi there,
>I need to carry out a database cleaning/maintenance operation whereby I
>take the text in one column called ShortDescription and add it to the
>text in a column called AdditionalDetails.
>I know how to copy from one to the other but that would have the effect
>of overwriting the values in the AdditionalDetails column. I need
>whatever is in the first column to be simply appended to whatever is in
>the second. If the first has null in it, nothing should happen, and I
>guess if the second has a null in it, the null should be replaced with
>whatever's in the second column.
>Can anyone advise me on how to do this? I'm sure it must be a relatively
>simple operation but I've never seen it done.
>Thanks to anyone who can help
>Simon|||You are both SQL Stalions!
I knew it would be simple!
Thank you - you've been a great help
Kindest Regards
Simon