5 Replies - 809 Views - Last Post: 15 January 2015 - 07:25 AM Rate Topic: -----

#1 SILENTSHER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-July 08

write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 04:41 AM

Hi i have 2 table , i have to get the value of sc_code from one table to other table
this is my first table from which i want to get the value of sc_code on katcode
means i have AB as katcode but i want the sc_code of AB to be written in other table how can i do that


   sno | sc_code | Name | katcode
   1   | 01      | ABC  | AB
   2   | 02      | XYZ  | XZ



Is This A Good Question/Topic? 0
  • +

Replies To: write data from one table to another in sqlserver 2008

#2 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 04:53 AM

Hi

You can use an INSERT statement with a SELECT statement to take the data from your first table and insert into your second table. The SQL statement would be something like:

INSERT INTO YourOtherTable (ColumnNameForSCCode) SELECT sc_code FROM YourFirstTableName WHERE katcode='AB'




HTH
Was This Post Helpful? 0
  • +
  • -

#3 SILENTSHER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-July 08

Re: write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 05:10 AM

if i have to select all the values from katcode and insert in sc_code of other table what changes should i have to make
Was This Post Helpful? 0
  • +
  • -

#4 djjeavons   User is offline

  • D.I.C Regular
  • member icon

Reputation: 114
  • View blog
  • Posts: 417
  • Joined: 09-January 09

Re: write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 05:17 AM

Not sure I follow. The statement that I posted will take all sc_code from your first table that have a katcode of 'AB'. So if there are five records then five records will be inserted into your other table.

Can you post the names of both tables (source and destination) and the fields that you want to copy from source to destination along with their data types.
Was This Post Helpful? 0
  • +
  • -

#5 SILENTSHER   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 16-July 08

Re: write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 05:54 AM

Actually Sir, i am working on the same project that i have asked yesterday
tis is the actual txt file which i want to insert into the databse table name "mktrate"
and i have created a function that read all the values from txtfile to a table called " MyTable"
now i want to insert that table into my database,
and i have other table named as " Company" so i have to get the sc_code from company table instead of katcode in "MyTable" i have to insert the sc_code to mktrate
i am sharing the screenshots of both the table
and this is the function to read the textfile
Public Function ReadMarketRate(ByVal FileName As String) As DataTable
        '----------------------------------------------------
        'Reads a csv file into a datatable, with the first row as column headers
        'Dim myFile As String = FilePath & "\" & FileName
        Dim myFile As String = FileName
        Dim myTable As DataTable = New DataTable("MyTable")
        Dim i As Integer = 0
        Dim myRow As DataRow
        Dim myColumn As DataColumn
        Dim MyType As String
        Dim fieldValues As String()
        Dim ColumnNames As String()
        Dim ColumnTypes As String()
        Dim myReader As New StreamReader(myFile)
        'SQuery = SQuery & "( tdate|InstCode,KSECode|InstName|orate,  "
        'SQuery = SQuery & "crate, "
        'SQuery = SQuery & "vol, "
        'SQuery = SQuery & "high, "
        ' lblProcess.Text = "Reading Market Rates"
        ProgressBar1.Minimum = 0
        'ProgressBar1.Maximum = 
        Dim sColumnName As String = "tdate|katcode|ksecode|instname|orate|high|low|crate|vol|v1|v2|v3"
        Dim sColumnType As String = "String|String|String|String|Double|Double|Double|Double|Double|String|String|String"

        'ColumnNames = myReader.ReadLine().Split("|")
        ColumnNames = sColumnName.Split("|")
        'ColumnTypes = myReader.ReadLine().Split("|")
        ColumnTypes = sColumnType.Split("|")
        Try
            'Open file and read first two lines
            'Create data columns named according to first line of data, with type according to second line
            Dim k As Integer = 0
            For k = 0 To ColumnNames.Length() - 1
                myColumn = New DataColumn()
                MyType = "System." & ColumnTypes(k)
                myColumn.DataType = System.Type.GetType(MyType)

                myColumn.ColumnName = ColumnNames(k)
                myTable.Columns.Add(myColumn)
            Next
            'Read the body of the data to data table
            ProgressBar1.Maximum = myReader.Peek()
            '  Dim i As Integer = 0
            While myReader.Peek() <> -1
                fieldValues = myReader.ReadLine().Split("|")
                myRow = myTable.NewRow
                For i = 0 To fieldValues.Length() - 1
                    If i = 0 Then
                        myRow.Item(i) = fieldValues(i).ToString.Substring(0, 2) & "-" & fieldValues(i).ToString.Substring(2, 3) & "-" & fieldValues(i).ToString.Substring(5, 4)
                    Else
                        myRow.Item(i) = fieldValues(i).ToString
                    End If
                Next
                myTable.Rows.Add(myRow)
                i = i + 1
                ProgressBar1.Value = i
            End While
        Catch ex As Exception
            MsgBox("Error building datatable : " & ex.Message)
            Return New DataTable("Empty")
        Finally
            myReader.Close()
        End Try

        Return myTable
    End Function


Attached image(s)

  • Attached Image
  • Attached Image

Attached File(s)

  • Attached File  data.txt (31.63K)
    Number of downloads: 86

Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • RequestedRangeNotSatisfiable
  • member icon

Reputation: 6561
  • View blog
  • Posts: 26,600
  • Joined: 12-December 12

Re: write data from one table to another in sqlserver 2008

Posted 15 January 2015 - 07:25 AM

In answer to your original question, you need an UPDATE statement to update existing records, joining the tables by the katcode:
UPDATE Table2 INNER JOIN Table1 ON Table2.katcode = Table1.katcode SET Table2.sc_code = Table1.sc_code

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1