Transact SQL :: How To Update Table In Many To Many Relationship

Nov 30, 2015

I am having challenge to update the redemption table from the multiple card activation table.  I want to update the redemption table with the activation date closest to the redeem date.  

For example:  Redeem date 20071223, I need to update the top row Date, Year, Period fields from the Card activation table.

Redeem date 20071228, I want to refer to the second row in the Card activation table date 20071228.  
Redeem date 20080316 or later, I want to use the last row in the card activation table date 20080316.

How to modify the update query to select the right activation row accordingly?

Below is my partial code I used but it always pick the 20071223 date to update my redemption table.

 CREATE TABLE #Card
 (
       [CardNumber] varchar(20)
 ,[ Date] int
 ,[ Year] int
 ,[ Month] int
 ,[ Period] int
  )  
 
[Code] ....

View 13 Replies


ADVERTISEMENT

Transact SQL :: Firing After Update Trigger - On Table Row Update

Jul 8, 2015

I have a table where table row gets updated multiple times(each column will be filled) based on telephone call in data.
 
Initially, I have implemented after insert trigger on ROW level thinking that the whole row is inserted into table will all column values at a time. But the issue is all columns are values are not filled at once, but observed that while telephone call in data, there are multiple updates to the row (i.e multiple updates in the sense - column data in row is updated step by step),

I thought to implement after update trigger , but when it comes to the performance will be decreased for each and every hit while row update.

I need to implement after update trigger that should be fired on column level instead of Row level to improve the performance?

View 7 Replies View Related

Transact SQL :: Update Table With Its Value And Data From Row In Temp Table For Matching Record?

Oct 25, 2015

I have a temp table like this

CREATE TABLE #Temp
 (
  ID int,
  Source varchar(50),
  Date datetime,
  CID varchar(50),
  Segments int,
  Air_Date datetime,

[code]....

Getting Error

Msg 102, Level 15, State 1, Procedure PublishToDestination, Line 34 Incorrect syntax near 'd'.

View 4 Replies View Related

Transact SQL :: Update Table Based On Available Date Range In Same Table

Dec 2, 2015

I would like to update the flag of the promotion ID should the promotion ID date range overlap with Promotion ID(All) Date Range. The general logic is as below.

Update TableName
SET PromotionID Flag = 1 AND Reason = 'Overlap with row ID(Overlap row ID number)'
Where EACH ROW(Except with Promotion ID ALL) Date Range Overlap with ROW(with promotion ID ALL) Date range

Note: ROW is Partition By ColumnA,ColumnB

TableName: PromotionList

ID PromotionID StartDate EndDate ColumnA ColumnB Flag Reason
1 1 2015-04-05 2015-05-28 NULL NULL 0 NULL
2 1 2015-04-05 2015-04-23 2 3 0 NULL
3 2 2015-05-04 2015-07-07 2 3 0 NULL
4 ALL 2015-04-05 2015-04-28 NULL NULL 0 NULL
5 ALL 2015-07-06 2015-07-10 2 3 0 NULL
6 1 2015-02-03 2015-03-03 NULL NULL 0 NULL

Expected outcome after performing update on the table

ID PromotionID StartDate EndDate ColumnA ColumnB Flag Reason
1 1 2015-04-05 2015-05-28 NULL NULL 1 Overlap with row ID 4
2 1 2015-04-05 2015-04-23 2 3 0 NULL
3 2 2015-05-04 2015-07-07 2 3 Overlap with row ID 5
4 ALL 2015-04-05 2015-04-28 NULL NULL 0 NULL
5 ALL 2015-07-06 2015-07-10 2 3 0 NULL
6 1 2015-02-03 2015-03-03 NULL NULL 0 NULL

View 4 Replies View Related

Transact SQL :: Update Multiple Table Referencing New Table Data

Aug 4, 2015

I have a table called ADSCHL which contains the school_code as Primary key and other two table as

RGDEGR(common field as SCHOOl_code) and RGENRl( Original_school_code) which are refrencing the ADSCHL. if a school_code will be updated both the table RGDEGR (school_code) and RGERNL ( original_schoolcode) has to be updated as well. I have been provided a new data that i have imported to SQL server using SSIS with table name as TESTCEP which has a column name school_code. I have been assigned a task to update the old school_code vale ( ADSCHL) with new school_code ( TESTCEP) and make sure the changes happen across all 3 tables.

I tried using Merge Update function not sure if this is going to work.

Update dbo.ADSCHL
SET dbo.ADSCHL.SCHOOL_CODE = FD.SCHOOL_Code
FROM dbo.ADSCHL AD
INNER JOIN TESTCEP FD
ON AD.SCHOOL_NAME = FD.School_Name

View 10 Replies View Related

Transact SQL :: Update Table From Linked Table If Something Has Changed?

May 20, 2015

Is it possible to check/notify if something (record) has changed in the linked table (from linked server) and automatically update table in local one?

View 4 Replies View Related

Transact SQL :: Update A Field In Table Based On Another Table

Nov 17, 2015

I have a daily record table (has field Date1, field2Update) and have another table(has field Date2, Flag (value: 0 and non 0 values)

I want to update table1 and set field2Update as somevalue where table2.Flag <> 0

View 4 Replies View Related

Transact SQL :: Update Table From Another Table For Multiple Rows

Jun 10, 2015

Matrix table has ID column and data below.

ID        Flag     TestDate         Value     Comment                                                                  
111          2       12/15/2014     7.5             null
222         2            Null                10          received

Matrix_Current table could have 1 or multiple rows as below.

ID        Flag               TestDate           Value         Comment
111         2                  01/26/2015        7.9                                                                      
111         2                  02/23/2015        7.9                                                      
111         2                  04/07/2015        6.8
222        1                   null                   8               test comment 1
222        3                   null                   9               test comment 2

When I run below update

 UPDATE  AM
 SET  M.Flag = MC.Flag, M.TestDate = MC.TestDate,
M.Value = MC.Value, M.comment = MC.Comment
 FROM dbo.Matrix M inner join dbo.Matrix_Current MC on M.ID = MC.ID

Matrix table has value below:

ID        Flag     TestDate         Value     Comment                                                                  
111          2       01/26/2015      7.9             
222         1            Null               8            test comment 1

I want to update Matrix table from all row from Matrix_Current, final table would like below:

ID        Flag     TestDate        Value     Comment                                                                  
111          2        04/07/2015      6.8             
222         3            Null                9         test comment 2

View 3 Replies View Related

Transact SQL :: Update One Table With Records From Another Table

Sep 5, 2015

How to update tblA with records from tblB (see below expected results) when they have common column DrawingNo?

View 4 Replies View Related

Transact SQL Question: How Do I Update A Table With Just One Row From Another Table.

Mar 14, 2008



I have 2 tables which with one-many relation. Table B has three child records based on Table A. I want to update value in Table from one of the records in table B.

Right now I am thinking of creating 2 temp table importing values there but does not seem to work. Can someone please help thanks. Armoghan

View 4 Replies View Related

Transact SQL :: Update Table From Another With Conditions

Dec 2, 2015

I am trying to do a simple update in one sql table from another where a certain condition in both tables are met.I am missing something basic and have tried numerous posted examples, without success.Here is my code (that does not update) :

        opdragaliasnaaminsit.Connection = konneksie
        opdragaliasnaaminsit.CommandText = "UPDATE vyfjaarontledings " & _
        "SET aliasnaam = T2.aliasnaam" & _
        " FROM  vyfjaarontledings T1" & _
        " INNER " & _
        "JOIN blokke T2 " & _
        " ON T1.plaasno = T2.plaasno " & _
         "WHERE T1.plaasno = T2.plaasno"
opdragaliasnaaminsit.ExecuteNonQuery()

I am trying to update aliasnaam in vyfjaarontledings from blokke.

View 10 Replies View Related

Transact SQL :: How To Update Table Without Using While Loop

Nov 15, 2015

I have two tables i have to update table2 using table1 without using while loop.

example given below.

Table1 

rid
id
amt
firdate
lastdate

1
1
500

[code]....

View 7 Replies View Related

Transact SQL :: Table Doesn't Get Update Through Job

Sep 28, 2015

I have following T-SQL statement:- 

insert into [dbo].[tbl_FG_Alert_Count_All_Report] ([Date] ,[Count] ,[Rule Type])
   SELECT TOP 10 [Date]
      ,[Count]
      ,[Rule Type]
  FROM [dbo].[tbl_FG_Alert_Count_All] where Count <>'0' and DATEDIFF(dy,date,GETDATE())  = 1 order by Date desc

When I ran this T-SQL statement in SSMS; I don't get any error and as expected, I can see new data in [dbo].

[tbl_FG_Alert_Count_All_Report] table. 

Now I created one job with same T-SQL Statement. Job completes successfully with out giving any error message; But unfortunately I don't see any new data in [dbo].[tbl_FG_Alert_Count_All_Report] table.  What would be the reason that I don't see new data when job completes successfully but I can see new data after executing same T-SQL statement in SSMS?

View 3 Replies View Related

Transact SQL :: Update All Records In Table?

Oct 22, 2015

Every day the timestamp is changed on all rows in one of the table(table has 1 mio records).

What is the best way to find out which query is doing this?

Could be also query which updates only one record(or couple of them) but is executed for each record once.

I have been looking sys.dm_exec_query_stats and sys.dm_exec_sql_text for past but couldn't find.

I can't use event notifications since there is service broker disabled.

View 5 Replies View Related

Transact SQL :: How To Create One To Many Relationship

Jul 29, 2015

I have got a table as follow:

CREATE TABLE [dbo].[NozzleAllTbl] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[nBDId] INT NULL,
[nShellId] INT NULL,
[nTDId] INT NULL,
CONSTRAINT [PK_dbo.NozzleAllTbl] PRIMARY KEY CLUSTERED ([Id] ASC),
CONSTRAINT [FK_dbo.NozzleAllTbl_dbo.NozzleTbl_nBDId] FOREIGN KEY ([nBDId]) REFERENCES [dbo].[NozzleTbl] ([Id]),
CONSTRAINT [FK_dbo.NozzleAllTbl_dbo.NozzleTbl_nShellId] FOREIGN KEY ([nShellId]) REFERENCES [dbo].[NozzleTbl] ([Id]),
CONSTRAINT [FK_dbo.NozzleAllTbl_dbo.NozzleTbl_nTDId] FOREIGN KEY ([nTDId]) REFERENCES [dbo].[NozzleTbl] ([Id])
);

NozzleTbl is another Table.At the moment I am referencing only one NozzleTbl thrice. But how can I reference a collection of NozzleTbl thrice?So that in the c# code I could access the collections as:

NozzleTbl_1 = NozzleAllTbl.NozzleTbls[0]
NozzleTbl_2 = NozzleAllTbl.NozzleTbls[1]
...
NozzleTbl1_1 = NozzleAllTbl.NozzleTbls1[0]
NozzleTbl2_1 = NozzleAllTbl.NozzleTbls2[0]
....

View 2 Replies View Related

Transact SQL :: Column Update With Comparison With Another Table

Nov 10, 2015

I upload data from a Txt File(Txt_Temp) where I have VinNumber with 6 digits. Another table name Resrve_Temp1 where I have Vinumber with 17 digit. Now I need to update the vinnumber 6 digit to 17 digit or to new column in Txt_temp.

Txt_Temp - Table

I tried this code with no succes and only one row is updating

update Txt_Temp Set Txt_Temp.Vinnumber=dbo.R_ResrvStock.Vin
from dbo.R_ResrvStock inner join Txt_Temp on Right (dbo.R_ResrvStock.Vin,6)=Txt_Temp.VinNumber

OR Add this code in view 

Select dbo.R_ResrvStock.Vin,R_Txt_Temp.Vinnumber,R_Txt_Te mp.Model_Code 
from dbo.R_ResrvStock inner join R_Txt_Temp on Right (dbo.R_ResrvStock.Vin,6)=R_Txt_Temp.VinNumber

Vin
123456
123123
123789

Resrve_Temp1 - Table
asddfghjklk123654
asddfghjklk123456
asddfghjklk321564
asddfghjklk123123
asddfghjklk123789
asddfghjklk654655
asddfghjklk456465

My Result can be in Txt_Temp table or new table or with one or two columns

asddfghjklk123456 123456
asddfghjklk123123 123123
asddfghjklk123789 123789

View 10 Replies View Related

Transact SQL :: Update Table Based On Join

Sep 7, 2015

I have table A with colums ID and Product. I have table B with ID (same as table A) and revenue of product.

I need to update the field Product of table A with the revenue of table B.

I'm confuse with the update joining both tables. I tried this, but obviously has an error:

Update A set Product=B.Revenue where A.ID=B.ID

View 6 Replies View Related

Transact SQL :: How To Update Multiple Records In A Table

Jul 23, 2015

I need to update more than one record at once. I have ~ 100 records that I have to update and don't want to execute query 100 times.

My query looks like this:

Update Table1
Set Table1.field1 = ( select Table2.field1 from Table2 where Table2.field2 IN ('a', 'b', 'c')
where Table1.field2 IN ( 'a', 'b', 'c')

It obviously failed because subquery returned more than one value and error message stated that I can't use '=' operator in this case.

My question: how could I update the same column from many records in one execution?

View 10 Replies View Related

Transact SQL :: Nested Cursors To Update Table?

May 15, 2015

I have been wrestling with the code all day to accomplish the following: I need to update a table based on values from another table. So far, I have been able to do the below:

DECLARE @LookUpTerm VARCHAR(25)
, @SearchCol VARCHAR(255)
, @LogonIDToProcess VARCHAR(50)
, @Matched CHAR
, @Cycle INT = 1
IF OBJECT_ID('tempdb..#Glossary','U') IS NOT NULL DROP TABLE #Glossary
IF OBJECT_ID('tempdb..#Employees','U') IS NOT NULL DROP TABLE #Employees

[code]...

View 7 Replies View Related

Transact SQL :: How To Create A 0-Many Relationship Between Two Tables

May 25, 2015

I have two tables, Inspections and InspectionDetails. The InspectionDetails only contains information if an inspection found violations. Some inspections don't look for violations so inspectiondetails would have no entry.How can I create a 0-Many relationship between these two tables?Inspection Table has : InspectionID, InspectionDate, TypeofInspection, Inspector..InspectionDetail Table has : DetailID (an auto-incrementing identity column), InspectionID, SectionViolated, ViolationDescription, InspectorNotes.

View 6 Replies View Related

Transact SQL :: Iterate Through Table Rows And Do Conditional Update

Aug 11, 2015

I have a table with the following fields:

ID (int, identity)
Name (nvarchar(255))
Block (nvarchar(50))
Street (nvarchar(255))
Floor (nvarchar(50))
Unit (nvarchar(50))
Address1 (nvarchar(255))
Address2 (nvarchar(255))

I want to iterate through the table and populate Address1 as [Block] [Street] #[Floor]-[Unit].If the 'Floor' field contain a number < 10 (e.g., '8'), I want to add a '0' before it (e.g., '08'). Same for Unit.How would I do this using cursors (or other recommended method)?

View 4 Replies View Related

Transact SQL :: Failure Update Table Column Script

Jul 17, 2015

I am dynamically creating a job using sql script and it does work fine(It creates the job and when it's done it gets deleted as it's created dynamically. so I won't be having any job history at all in the system.). I want to update an existing table if the jobs fails and I am not sure how I can do that using t-sql script. Is that possible?I have an idea but not sure whether it works. In the job create script, can I include the code for if the job fails then go to step 2 and update the table column with the error??? If so, how can I retrieve the error???

View 3 Replies View Related

Transact SQL :: Update Records With Minimum Date In Table?

Nov 19, 2015

I want to update the STATUS column based on min date in my table.

For example – If minimum BOOKING_DATE of any RecieptID is below to 2015-10-01, then Status of that RecieptID should be 0 for all records pertaining to dedicated RecieptID So I need an output in this way.

View 3 Replies View Related

Transact SQL :: Update A Column Randomly Based On Values From Another Table

Oct 27, 2015

I'm trying to Update a column table based on values from another table but I need these values are random.My query looks like this and doesn´t work

DECLARE @Rowini int,
DECLARE @lastrow int
SET @Rowini = 1
SET @Lastrow = 80000

[code]...

View 8 Replies View Related

Transact SQL :: Update Table With Max Value And Row Number (based On 2 Column Partitions)

Sep 15, 2015

I have 3 columns. I would like to update a table based on job_cd and permit_nbr column. if we have same job_cd and permit_nbr, reference number should be same else it should take max(reference number) from the table +1 for all rows where reference_nbr column is null

job_cd permit_nbr reference_nbr

ABC1 990 100002
ABC1 990 100002
ABC1 991 100003
ABC1 992 100004
ABC1 993 100005
ABC2 880 100006
ABC2 881 100007
ABC2 881 100007
ABC2 882 100008
ABC2 882 100008

View 3 Replies View Related

Transact SQL :: Query To Update A Table With More Than 150 Million Rows Of Data?

Sep 17, 2015

I have been tasked with writing an update query to update a table with more than 150 million rows of data. Here are the table structures:

Source Tables :

OC
CREATE TABLE [dbo].[OC](
[OC] [nvarchar](255) NULL,
[DATE DEBUT] [date] NULL,
[DATE FIN] [date] NULL,
[Code Article] [nvarchar](255) NULL,
[INSERTION] [nvarchar](255) NULL,

[Code] ....

The update requirement is as follows:

DECLARE @Counter INT=0 --This causes the @@rowcount to be > 0
while @@rowcount>0
BEGIN
    SET rowcount 10000
    update r
    set Comp=t.Comp

[Code] ....

The update took more than 48h and didn't terminate , how to accelerate it ?

View 6 Replies View Related

Transact SQL :: How To Update Multiple Rows In Different Transactions In A Single Table

Jul 16, 2015

We have control table which will be useful whether we need to start the job or not. If we are starting the Job we will make it to 1.

Below is the Table Structure.

Table Name       IN_USE_FG
CUST_D                     0
PROD_D                     0
GEO_D                       0
DATE_D                     0

Now we have different packages for 4 tables data loading. These 4 packages will start at a time. Before going to load the data we have to make the Flag to 1 and after that we have to load it. Because of this we have written Update statement to update the Value to 1 in respective Package. 

Now we are getting dead lock because we are using same table at a same time. Because we are updating different records. 

View 6 Replies View Related

Transact SQL :: How To Update New Table By Separating Single Date Column

Oct 11, 2013

I am trying to build a DIM table using a source table that has the following setup...

CREATE TABLE [dbo].[APPL_STATUSES](
[APPLICATIONS_ID] [varchar](10) NOT NULL,
[POS] [decimal](10, 0) NOT NULL,
[APPL_STATUS] [varchar](5) NULL,
[APPL_STATUS_DATE] [datetime] NULL,
[APPL_APPLICANT] [varchar](10) NULL)
GO

[code]....

What I am trying to do is to break out the APPL_STATUS_DATE into a STATUS_START_DATE and an STATUS_END_DATE in a new table.  I also need to be able to update the STATUS_END_DATE based on the previous day's date. Like so...

CREATE TABLE [dbo].[APPL_APPLICANT_STATUSES](
[APPLICATIONS_ID] [varchar](10) NOT NULL,
[POS] [decimal](10, 0) NOT NULL,
[APPL_STATUS] [varchar](5) NULL,
[STATUS_START_DATE] [datetime] NULL,
[STATUS_END_DATE] [datetime] NULL,
[APPL_APPLICANT] [varchar](10) NULL)
GO

[code]...

View 10 Replies View Related

Transact SQL :: Want To Update One Table Using Other Table

Nov 23, 2015

I have two table  i want to update one table using other table.query given below

--table 1

create table #temp_P
(P_id int null,
p_name varchar(50),
h_id int)

--table 2

which is to be updated,check the p_name from above table( like 1 has same name like 4 so 1 will replace by 4)
create table #temp_t
(t_id int null,
p_id varchar(50),
r_id int)

[code]...

View 5 Replies View Related

How Can I Update Relationship Tables?

May 14, 2007

<----------I
have 2 tables are: 'customers (parent)' and 'open_ac (child)'

<--------I
have tried to insert and update data into sql database by using textboxes
(don't use datagrid)

<--------My
tables details are below
 

<-------this
table uses for keeping user data 

customers
fields:  

Column
name          
        type  
        length     
                 
             Description

cu_id  
               
             int  
             4  
         Primary key     
      Identifiers

cu_fname
                
    nvarchar   
       20        
  allow null          
     first name       
                 
     

cu_lname
                 
   nvarchar         
40            allow null 
              last name

cu_nat
                
        nvarchar      
  
20            allow
null               
nationality

cu_add  
               
      nvarchar      
  
40            allow null             
  address

cu_wplace
            
      nvarchar      
  
40            allow
null          
     workplace

cu_tel
             
           nvarchar   
      10
           allow
null               
telephone

cu_fax
             
          nvarchar   
      10
           allow null          
         fax

cu_email
             
       nvarchar        
10            allow
null             
     email

 <----the
open_ac uses for keeping register date/time of customers

open_ac
fields:  

Column
name           type  
        length           
Description

cu_id  
               
    int           
     4           
      link key

op_date  
            date/time          
8                
register date

 

<----------my
code 

Imports
System.Data.SqlClient

Public Class cus_reg
    Inherits System.Web.UI.Page
    Dim DS As DataSet
    Dim iRec As Integer   'Current Record
    Dim m_Error As String = ""

    Public Property MyError() As String
        Get
            Return
m_Error
        End Get
        Set(ByVal Value As String)
            m_Error =
Value
            If
Trim(Value) = "" Then
               
Label3.Visible = False
            Else
               
Label3.Text = Value
               
Label3.Visible = True
            End If
        End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles MyBase.Load
        If Not Page.IsPostBack Then
            Dim C1 As
New MISSQL
            'DS =
C1.GetDataset("select * from customers;select * from open_ac;select * from
accounts")
            DS =
C1.GetDataset("select * from customers;select * from open_ac")
           

   
        Session("data") = DS
            iRec = 0
           
Viewstate("iRec") = iRec
           
Me.MyDataBind()

            Dim Dtr As
DataRow = DS.Tables(0).NewRow
           
DS.Tables(0).Rows.Add(Dtr)
            iRec =
DS.Tables(0).Rows.Count - 1
           
viewstate("iRec") = iRec
           
Me.Label2.Text = DateTime.Now
           
Me.MyDataBind()
        Else
            DS =
Session("data")
            iRec =
ViewState("iRec")
        End If
        Me.MyError = ""
    End Sub


    Public Function BindField(ByVal FieldName As String) As
String
        Dim DT As DataTable = DS.Tables(0)
        Return DT.Rows(iRec)(FieldName)
& ""
    End Function
    Public Sub MyDataBind()
        Label1.Text = "Record : "
& iRec + 1 & " of " & DS.Tables(0).Rows.Count
        txtcu_id.DataBind()
        txtcu_fname.DataBind()
        txtcu_lname.DataBind()
        txtcu_add.DataBind()
        txtcu_occ.DataBind()
        txtcu_wplace.DataBind()
        txtcu_nat.DataBind()
        txtcu_tel.DataBind()
        txtcu_fax.DataBind()
        txtcu_email.DataBind()  
    End Sub
   

Here is
update code


    Private Sub bUpdate_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles bUpdate.Click
        Dim DT As DataTable = DS.Tables(0)
        Dim DR As DataRow = DT.Rows(iRec)
        'Dim DR1 As DataRow = DT1.Rows(iRec)

        If DR.RowState = DataRowState.Added
Then
            If
txtcu_id.Text.Trim = "" Then
               
Me.MyError = "please enter your  id"
               
Exit Sub
            End If
            DR("cu_id")
= txtcu_id.Text
        End If

        If txtcu_fname.Text.Trim =
"" Then
            Me.MyError =
"please enter your name"
            Exit Sub
        Else
           
DR("cu_fname") = txtcu_fname.Text
        End If

        If txtcu_lname.Text.Trim =
"" Then
            Me.MyError =
"please enter your last name"
            Exit Sub
        Else
           
DR("cu_lname") = txtcu_lname.Text
        End If

        If txtcu_add.Text.Trim =
"" Then
            Me.MyError =
"please enter your address"
            Exit Sub
        Else
           
DR("cu_add") = txtcu_add.Text
        End If

        If txtcu_occ.Text.Trim =
"" Then
            Me.MyError =
"please enter your occupation"
            Exit Sub
        Else
           
DR("cu_occ") = txtcu_occ.Text
        End If

        If txtcu_wplace.Text.Trim =
"" Then
            Me.MyError =
"please enter your workplace"
            Exit Sub
        Else
           
DR("cu_wplace") = txtcu_wplace.Text
        End If

        If txtcu_nat.Text.Trim =
"" Then
            Me.MyError =
"Please enter your nationality"
            Exit Sub
        Else
           
DR("cu_nat") = txtcu_nat.Text
        End If

        If txtcu_tel.Text.Trim =
"" Then
           
DR("cu_tel") = DBNull.Value
        Else
           
DR("cu_tel") = txtcu_tel.Text
        End If

        If txtcu_tel.Text.Trim =
"" Then
           
DR("cu_fax") = DBNull.Value
        Else
           
DR("cu_fax") = txtcu_fax.Text
        End If

        If txtcu_email.Text.Trim =
"" Then
           
DR("cu_email") = DBNull.Value
        Else
           
DR("cu_email") = txtcu_email.Text
        End If
        
        Dim Strsql As String
        If DR.RowState = DataRowState.Added
Then
            Strsql =
"insert into customers (cu_id,cu_fname,cu_lname,cu_add,cu_occ,cu_wplace,cu_nat,cu_tel,cu_fax,cu_email)
values (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10)"
        Else
            Strsql =
"update customers set
cu_fname=@P2,cu_lname=@P3,cu_add=@P4,cu_occ=@P5,cu_wplace=@P6,cu_nat=@P7,cu_tel=@P8,cu_fax=@P9,cu_email=@P10
where cu_id =@P1"
        End If
        Dim C1 As New MISSQL
        Dim cmd As SqlCommand =
C1.CreateCommand(Strsql)
        C1.CreateParam(cmd,
"ITTTTTTTTT")
       
cmd.Parameters("@P1").Value = DR("cu_id")
        cmd.Parameters("@P2").Value
= DR("cu_fname")
       
cmd.Parameters("@P3").Value = DR("cu_lname")
       
cmd.Parameters("@P4").Value = DR("cu_add")
       
cmd.Parameters("@P5").Value = DR("cu_occ")
       
cmd.Parameters("@P6").Value = DR("cu_wplace")
        cmd.Parameters("@P7").Value
= DR("cu_nat")
       
cmd.Parameters("@P8").Value = DR("cu_tel")
       
cmd.Parameters("@P9").Value = DR("cu_fax")
       
cmd.Parameters("@P10").Value = DR("cu_email")
      

        Dim Y As Integer = C1.Execute(cmd)
        If Y > 0 Then
           
DR.AcceptChanges()
        Else
            Me.MyError =
"Can not register"
        End If
<---------code above in this sub it can update only customers tables and when I tried to coding below<------------it alerts can not update 
        Dim DT1 As DataTable = DS.Tables(1)
        Dim DR1 As DataRow = DT1.Rows(iRec)
        If DR1.RowState = DataRowState.Added
Then
            If
txtcu_id.Text.Trim = "" Then
               
Me.MyError = "Please enter id"
               
Exit Sub
            End If
            DR1("cu_id")
= txtcu_id.Text
        End If
        If Label2.Text.Trim = ""
Then
           
DR1("op_date") = Label2.Text
        End If

        Dim StrSql1 As String
        If DR1.RowState =
DataRowState.Deleted Then
            StrSql1 =
"insert into open_ac (cu_id,op_date) values (@P13,@P14)"
        Else
            StrSql1 =
"update open_ac set op_date=@P14 where cu_id=@P13"
        End If
        Dim C2 As New MISSQL
        Dim cmd2 As SqlCommand =
C2.CreateCommand(StrSql1)
        C2.CreateParam(cmd2, "ID")
       
cmd2.Parameters("@P1").Value = DR1("cu_id")
       
cmd2.Parameters("@P2").Value = DR1("op_date")

        Dim Y1 As Integer = C2.Execute(cmd2)
        If Y1 > 0 Then
           
DR1.AcceptChanges()
        Else
            Me.MyError =
"Can not register"
        End If
    End Sub
End Class 

<------this
is class  I  use for connecting to database and call parameters....

MISSQL
class

Imports
System.Data.SqlClient
Public Class MISSQL
    Dim PV As String =
"Server=web_proj;uid=sa;pwd=sqldb;"
    Dim m_Database As String = "c1_itc"
    Public Strcon As String
    Public Sub New()
        Strcon = PV &
"database=" & m_Database
    End Sub
    Public Sub New(ByVal DBName As String)
        m_Database = DBName
        Strcon = PV &
"database=" & m_Database
    End Sub
    Public Property Database() As String
        Get
            Return
m_Database
        End Get
        Set(ByVal Value As String)
            m_Database =
Value
            Strcon = PV
& "database=" & m_Database
        End Set
    End Property

    Public Function GetDataset(ByVal Strsql As String, _
        Optional ByVal DatasetName As String
= "Dataset1", _
        Optional ByVal TableName As String =
"Table") As DataSet
        Dim DA As New SqlDataAdapter(Strsql,
Strcon)
        Dim DS As New DataSet(DatasetName)
        Try
            DA.Fill(DS,
TableName)
        Catch x1 As Exception
           
Err.Raise(60002, , x1.Message)
        End Try
        Return DS
    End Function

    Public Function GetDataTable(ByVal Strsql As String, _
         Optional ByVal TableName As
String = "Table") As DataTable
        Dim DA As New SqlDataAdapter(Strsql,
Strcon)
        Dim DT As New DataTable(TableName)
        Try
            DA.Fill(DT)
        Catch x1 As Exception
           
Err.Raise(60002, , x1.Message)
        End Try
        Return DT
    End Function

    Public Function CreateCommand(ByVal Strsql As String) As
SqlCommand
        Dim cmd As New SqlCommand(Strsql)
        Return cmd
    End Function

    Public Function Execute(ByVal Strsql As String) As
Integer
        Dim cmd As New SqlCommand(Strsql)
        Dim X As Integer = Me.Execute(cmd)
        Return X
    End Function

    Public Function Execute(ByRef Cmd As SqlCommand) As
Integer
        Dim Cn As New SqlConnection(Strcon)
        Cmd.Connection = Cn
        Dim X As Integer
        Try
            Cn.Open()
            X =
Cmd.ExecuteNonQuery()
        Catch
            X = -1
        Finally
            Cn.Close()
        End Try
        Return X
    End Function

    Public Sub CreateParam(ByRef Cmd As SqlCommand, ByVal
StrType As String)
        'T:Text, M:Memo, Y:Currency,
D:Datetime, I:Integer, S:Single, B:Boolean, P: Picture
        Dim i As Integer
        Dim j As String
        For i = 1 To Len(StrType)
            j =
UCase(Mid(StrType, i, 1))
            Dim P1 As
New SqlParameter
           
P1.ParameterName = "@P" & i
            Select Case
j
               
Case "T"
                   
P1.SqlDbType = SqlDbType.NVarChar
               
Case "M"
                   
P1.SqlDbType = SqlDbType.Text
               
Case "Y"
                   
P1.SqlDbType = SqlDbType.Money
               
Case "D"
                   
P1.SqlDbType = SqlDbType.DateTime
               
Case "I"
                   
P1.SqlDbType = SqlDbType.Int
               
Case "S"
                   
P1.SqlDbType = SqlDbType.Decimal
               
Case "B"
                   
P1.SqlDbType = SqlDbType.Bit
               
Case "P"
                   
P1.SqlDbType = SqlDbType.Image
            End Select
           
Cmd.Parameters.Add(P1)
        Next
    End Sub
End Class
               
           
       

<-------Thank you in advance<-------and Thank you very much for all help

   
 

 

View 2 Replies View Related

Cannot Update ID Because Of No Existing Relationship

Mar 5, 2005

I have one table Phone and a table SmsMessage that are linked by the Cellnumber. Cellnumber is the primary key in Phone.

For some reason in the table Phone the Cellnumbers are stored with extra spaces like: '+27000000000 ', but in the SmsMessage table the same value is stored as '+27000000000'. However when I want perform an update to trim the Cellnumbers, I get the message 'Cannot modify values Cellnumber in Phone because there are dependent values in SmsMessage.

The fact is that there are no dependent values, but for MSSQL '+27000000000' and '+27000000000 ' is the same????! Note that the function Len(Cellnumber) gives me the length of the string WITHOUT the spaces as well.

Even if I remove all relationships from Phone, I still get the same error. Are there more places in MSSQL where relationships are stored besides the Diagrams?

Or is there a command that tells MSSQL to ignore all relationships for the next query?

Any ideas?

View 1 Replies View Related

Transact SQL :: Create Index On Temp Table To Reduce Run Time Of Update Query

Apr 29, 2015

I want to create index for hash table (#TEMPJOIN2) to reduce the update query run time. But I am getting "Warning!

The maximum key length is 900 bytes. The index 'R5IDX_TMP' has maximum length of 1013 bytes. For some combination of large values, the insert/update operation will fail". What is the right way to create index on temporary table.

Update query is running(without index) for 6 hours 30 minutes. My aim to reduce the run time by creating index. 

And also I am not sure, whether creating index in more columns will create issue or not.

Attached the update query and index query.

CREATE NONCLUSTERED INDEX [R5IDX_TMP] ON #TEMPJOIN2
(
[PART] ASC,
[ORG] ASC,
[SPLRNAME] ASC,
[REPITEM] ASC,
[RFQ] ASC, 

[Code] ....

View 7 Replies View Related

Transact SQL :: Update One Table Based On Another Table Values For Multiple Values

Apr 26, 2015

I have two tables  A(uname,address,full_name) and B(uname,full_name). I want to update table A for all matching case of uname in table B. 

View 5 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved