Auth2.dbo.Accounts : account_id, login_name , password , other columns...
Auth.dbo.Account : account_id , account , password , other columns...
I want to insert accounts(account_id,account,password) from "Auth.dbo.Account" to "Auth2.dbo.Accounts" (account_id,login_name , password) and giving other columns values that I want.
I was hoping that this could work , but once I wrote in SQL Management studio , I got an error syntax error near the " select account_id from Auth.dbo.account"
INSERT [dbo].[Accounts] ([account_id], [login_name], [password],
[referral_id], [referral_code], [pcbang], [block],
[withdraw_remain_time], [age], [auth_ok],
[last_login_server_idx], [event_code], [server_list_mask],
[result], [ip], [game_code], [gamecode], [login_event],
[email], [security_a_1], [security_a_2], [security_a_3],
[security_a_4], [security_q_1], [security_q_2], [security_q_3],
[security_q_4], [votepoints], [cash], [country])
VALUES (select account_id from auth.dbo.Account, N'Imad',
N'3cfbbd2ae3c3e416c6d00a5a12ee60e8', NULL, NULL,
0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL,
N'::1', NULL, NULL, NULL, N'imad.lekal#outlook.com',
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
0, 189, NULL)
Try with simple INSERT/SELECT. In this case account_id will be from auth.dbo.Account and all the other values will be constants.
INSERT Auth2.[dbo].[Accounts] ([account_id], [login_name], [password], [referral_id], [referral_code], [pcbang], [block], [withdraw_remain_time], [age], [auth_ok], [last_login_server_idx], [event_code], [server_list_mask], [result], [ip], [game_code], [gamecode], [login_event], [email], [security_a_1], [security_a_2], [security_a_3], [security_a_4], [security_q_1], [security_q_2], [security_q_3], [security_q_4], [votepoints], [cash], [country])
select account_id , N'Imad', N'3cfbbd2ae3c3e416c6d00a5a12ee60e8', NULL, NULL, 0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL, N'::1', NULL, NULL, NULL, N'imad.lekal#outlook.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 189, NULL from auth.dbo.Account;
Remove the VALUES (
When you INSERT using SELECT you should not use VALUES, since the values are coming from the SELECT
Also prefix the table names with the database names, assuming that Auth and Auth2 are 2 different databases.
Finally, your FROM is off.
Following should work.
INSERT Auth2.dbo.Accounts
(
[account_id]
, [login_name]
, [password]
, [referral_id]
, [referral_code]
, [pcbang]
, [block]
, [withdraw_remain_time]
, [age]
, [auth_ok]
, [last_login_server_idx]
, [event_code]
, [server_list_mask]
, [result]
, [ip]
, [game_code]
, [gamecode]
, [login_event]
, [email]
, [security_a_1]
, [security_a_2]
, [security_a_3]
, [security_a_4]
, [security_q_1]
, [security_q_2]
, [security_q_3]
, [security_q_4]
, [votepoints]
, [cash]
, [country]
)
SELECT
account_id
, N'Imad'
, N'3cfbbd2ae3c3e416c6d00a5a12ee60e8'
, NULL
, NULL
, 0
, 0
, NULL
, -1997
, 1
, NULL
, NULL
, NULL
, NULL
, N'::1'
, NULL
, NULL
, NULL
, N'imad.lekal#outlook.com'
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0
, 189
, NULL
FROM auth.dbo.Account
PRO TIP: Format your SQL properly bro, it helps to debug :).
hi i think this will help you first of all you create database link
CREATE DATABASE LINK your_db_link_name
CONNECT TO Schema_Name
IDENTIFIED BY <PWD>
USING 'Your Database Name';
next you need to make a query
INSERT [Accounts] ([account_id], [login_name], [password], [referral_id]
, [referral_code], [pcbang], [block], [withdraw_remain_time], [age], [
auth_ok], [last_login_server_idx], [event_code], [server_list_mask], [
result], [ip], [game_code], [gamecode], [login_event], [email], [
security_a_1], [security_a_2], [security_a_3], [security_a_4], [
security_q_1], [security_q_2], [security_q_3], [security_q_4], [
votepoints], [cash], [country])
VALUES (
select ac.account_id
from Your_DB_Link_Name#Account ac, N'Imad', N'3cfbbd2ae3c3e416c6d00a5a12ee60e8',
NULL, NULL, 0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL, N'::1', NULL,
NULL, NULL, N'imad.lekal#outlook.com', NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, 0, 189, NULL)
i think this will help you if you have any query then feel free
Related
I have a function that for some reason is not bringing back data in the table, but if I cut down the code and test it by itself it works.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[ReportingCyclesForReporting]
(
#ReportingCycleId BIGINT
)
RETURNS #t TABLE
(
[ReportingCycleId] [bigint] NOT NULL,
[BusinessId] [bigint] NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[ScenarioId] [bigint] NULL,
[SourceReportingCycleId] [bigint] NULL,
[MjMEPerDay] [decimal](19, 4) NOT NULL,
[AssetAppreciation] [decimal](5, 2) NULL,
[LandAssetAppreciation] [decimal](5, 2) NULL,
[OffFarmAssetAppreciation] [decimal](5, 2) NULL,
[RolledOverDate] [datetime] NULL,
[Version] [int] NOT NULL,
[Archived] [bit] NOT NULL,
[Essential] [bit] NOT NULL,
[EssentialsLeasedLandValue] [decimal](19, 4) NOT NULL,
[EssentialsShareFarmedLandValue] [decimal](19, 4) NOT NULL,
[EssentialsWaterValue] [decimal](19, 4) NOT NULL,
[RolloverDataOverwrite] [bit] NULL
)
AS
BEGIN
DECLARE #businessId INT
DECLARE #scenarioId INT
DECLARE #essential BIT
-- Get businessid and scenario id for this reporting cycle
SELECT #businessId = BusinessId
, #scenarioId = ScenarioId
, #essential = Essential
FROM ReportingCycle
WHERE Id = #ReportingCycleId
AND Archived = 0
BEGIN
IF #scenarioId IS NOT NULL
begin
-- Scenario reporting cycle siblings of the given reporting cycle
INSERT INTO #t(
ReportingCycleId
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
)
SELECT rc.Id
, rc.BusinessId
, rc.StartDate
, rc.EndDate
, rc.ScenarioId
, rc.SourceReportingCycleId
, rc.MjMEPerDay
, rc.AssetAppreciation
, rc.LandAssetAppreciation
, rc.OffFarmAssetAppreciation
, rc.RolledOverDate
, rc.Version
, rc.Archived
, rc.Essential
, rc.EssentialsLeasedLandValue
, rc.EssentialsShareFarmedLandValue
, rc.EssentialsWaterValue
FROM ReportingCycle
INNER JOIN ReportingCycle rc ON rc.ScenarioId = ReportingCycle.ScenarioId AND rc.Archived = 0
WHERE ReportingCycle.Id = #ReportingCycleId
AND ReportingCycle.Archived = 0
ORDER BY rc.StartDate
end
ELSE IF EXISTS(SELECT * FROM FutureReportingCycles(#businessId, #essential) WHERE Id = #reportingCycleId)
begin
-- Future reporting cycles
INSERT INTO #t(
ReportingCycleId
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
)
SELECT Id
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
FROM FutureReportingCycles(#businessId, #essential)
END
ELSE IF EXISTS(SELECT * FROM HistoryReportingCycles(#businessId, #essential) WHERE Id = #reportingCycleId and ScenarioId is null)
BEGIN
-- History reporting cycles
INSERT INTO #t(
ReportingCycleId
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
)
SELECT Id
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
FROM HistoryReportingCycles(#businessId, #essential) where ScenarioId is null
END
ELSE
-- Now
BEGIN
INSERT INTO #t(
ReportingCycleId
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
)
SELECT Id
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
FROM ReportingCycle WHERE id = #ReportingCycleId and Archived = 0
END
END
RETURN
END
I will execute this to trigger it to test
select * from ReportingCyclesForReporting(14212)
This happens when it's a future reporting cycle. I will get an empty table returned. I've debugged this in Visual Studio and I can see that it goes into the correct branch with the correct values but the table returns nothing.
But if I run a cut down version of the script brings back the values.
DECLARE #t TABLE
(
[ReportingCycleId] [bigint] NOT NULL,
[BusinessId] [bigint] NOT NULL,
[StartDate] [datetime] NOT NULL,
[EndDate] [datetime] NOT NULL,
[ScenarioId] [bigint] NULL,
[SourceReportingCycleId] [bigint] NULL,
[MjMEPerDay] [decimal](19, 4) NOT NULL,
[AssetAppreciation] [decimal](5, 2) NULL,
[LandAssetAppreciation] [decimal](5, 2) NULL,
[OffFarmAssetAppreciation] [decimal](5, 2) NULL,
[RolledOverDate] [datetime] NULL,
[Version] [int] NOT NULL,
[Archived] [bit] NOT NULL,
[Essential] [bit] NOT NULL,
[EssentialsLeasedLandValue] [decimal](19, 4) NOT NULL,
[EssentialsShareFarmedLandValue] [decimal](19, 4) NOT NULL,
[EssentialsWaterValue] [decimal](19, 4) NOT NULL,
[RolloverDataOverwrite] [bit] NULL
)
INSERT INTO #t(
ReportingCycleId
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
)
SELECT Id
, BusinessId
, StartDate
, EndDate
, ScenarioId
, SourceReportingCycleId
, MjMEPerDay
, AssetAppreciation
, LandAssetAppreciation
, OffFarmAssetAppreciation
, RolledOverDate
, Version
, Archived
, Essential
, EssentialsLeasedLandValue
, EssentialsShareFarmedLandValue
, EssentialsWaterValue
FROM FutureReportingCycles(306, 0)
select * from #t
This will bring back the values
Here is a picture of it in debug mode showing it goes into that branch.
I would really appreciate some help with this.
Here is my table
CREATE TABLE [dbo].[RPost_Receipts]
(
[id] [int] IDENTITY(1,1) NOT NULL,
[branch] [int] NULL,
[policyref] [varchar](10) NULL,
[receipt_email_received] [datetime] NULL,
[receipt_email_to_openattach] [int] NULL,
[receipt_email_to_openattach_dt] [datetime] NULL,
[sender_name] [varchar](50) NULL,
[sender_address] [varchar](100) NULL,
[subject] [varchar](160) NULL,
[message_id] [varchar](100) NULL,
[time_received] [datetime] NULL,
[delivery_to] [varchar](100) NULL,
[delivery_status] [varchar](100) NULL,
[delivery_report] [varchar](max) NULL,
[delivery_time_utc] [datetime] NULL,
[delivery_time_local] [datetime] NULL,
[time_opened] [datetime] NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
This is the following UPDATE query I am attempting to run on the same table
UPDATE [FreshSystems].[dbo].[RPost_Receipts]
SET [sender_name] = 'RPost eSignOff Service' ,
[sender_address] = 'contracts#usw.rpost.net' ,
[subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
[message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
[time_received] = '9/15/2015 10:36:29 AM' ,
[delivery_to] = 'AutoSaintCS#Fresh.co.uk' ,
[delivery_status] = 'Delivered to Mailserver' ,
[delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
[delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
[delivery_time_local] = '9/15/2015 10:36:47 AM' ,
[time_opened] = 'NULL'
WHERE [branch] = 02
AND [policyref] = 'OGKX02PC01'
AND [delivery_to] IS NULL
Why am I receiving a conversion error
Msg 241, Level 16, State 1, Line 1
Conversion failed when converting date and/or time from character string.
The inserts are going into 'DATETIME' columns and the datetime's that are being inserted are all correctly formatted, can anyone shed any light on this for me please?
UPDATE
The problem seems to occur ONLY within my VB.NET Application. When running the statement in SQL Server Management Studio it is fine, when it executed within my VB.NET it fails as a conversation from character to Datetime.
Any suggestions?
the issue is you were trying to put the word NULL into the time_opened field. (which is a datetime field)
Change it from [time_opened] = 'NULL' to [time_opened] = NULL
UPDATE [FreshSystems].[dbo].[RPost_Receipts]
SET [sender_name] = 'RPost eSignOff Service' ,
[sender_address] = 'contracts#usw.rpost.net' ,
[subject] = 'Re: REF: 02-OGKX02PC01 Your Insurance Policy (2 of 2)' ,
[message_id] = '49B918875098C1EFCB5A33FDB2D446FF5C294ACE' ,
[time_received] = '9/15/2015 10:36:29 AM' ,
[delivery_to] = 'AutoSaintCS#Fresh.co.uk' ,
[delivery_status] = 'Delivered to Mailserver' ,
[delivery_report] = '250 OK id=1ZbnbS-0003fY-4y engine03-30179-2.icritical.com (192.162.216.4)' ,
[delivery_time_utc] = '9/15/2015 10:36:47 AM' ,
[delivery_time_local] = '9/15/2015 10:36:47 AM' ,
[time_opened] = NULL
WHERE [branch] = 02
AND [policyref] = 'OGKX02PC01'
AND [delivery_to] IS NULL
I think Datetime format is yy-mm-dd hh:mm:ss and not 9/15/2015 10:36:29 AM
Convert it before updating table.
I am facing a big issue, the below stored procedure taking lot of time for execution. Please help me to find issues with following stored procedure .
We need to insert bulk subscriber list from excel in to database. But only 60 subscribers are getting inserted in to db in one minute.
Please help me to resolve the issue.
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile]
#JobCode varchar(25)
WITH RECOMPILE
AS
declare
#jobCode1 Varchar(50),
#count int,#Code varchar(50),#Name [varchar](50),#Date Datetime,#Status int,
#i int,#EUCount int,#SubCount int,
#Add1 nvarchar(3000) ,
#Add2 nvarchar(500) ,
#Add3 nvarchar(500)
,#refdate [varchar](50) ,
#reference [varchar](50) ,
#Joined [varchar](50),
#Joinmonth [nvarchar](50),
#Activated [varchar](50),
#ActivMonth [nvarchar](50),
#Center [varchar](50) ,
#Region [varchar](50) ,
#Area [varchar](50) ,
#Modem [varchar](50) ,
#Adomstatus [varchar](50)
, #AddCode [varchar](50)
set #i = 1
Set #jobCode1 =#JobCode
BEGIN
SET NOCOUNT ON
Set #Status = (Select Distinct(status) from TSMST1005 where Jobcode = #jobCode1)
if (#Status = 0)
begin
Select '1' as res
end
else
begin
CREATE TABLE #tblSMS(pID int identity(1,1),
[Reference] [nvarchar](50) NULL,
[Date] [varchar](50) NOT NULL,
[Code] [nvarchar](50) NULL,
[Subname] [varchar](500) NULL,
[Address1] [nvarchar](3000) NULL,
[Address2] [nvarchar](500) NULL,
[Address3] [nvarchar](500) NULL,
[Joined] [varchar](50) NULL,
[Joinmonth] [varchar](50) NULL,
[Activated] [varchar](50) NULL,
[ActivMonth] [nvarchar](50) NULL,
[Center] [varchar](50) NULL,
[Region] [varchar](50) NULL,
[Area] [varchar](50) NULL,
[Modem] [varchar](50) NULL,
[Adomstatus] [varchar](50) NULL,
[RefDate] [varchar](50) NOT NULL)
insert into #tblSMS
SELECT Reference,[Date],
Code,Subname ,Address1 ,Address2 ,Address3 ,
Joined , Joinmonth ,Activated, ActivMonth ,
Center,Region,Area,Modem,Adomstatus ,refdate FROM TSMST1005 where jobcode = #jobCode1 and Status =1
WHILE #i <= (SELECT COUNT(*) FROM #tblSMS)
BEGIN
SELECT
#Code =Code,
#Name = Subname,
#Date =[Date],
#Add1 =Address1 ,
#Add2 =Address2 ,
#Add3= Address3,
#reference =Reference ,
#Joined = Joined,
#Joinmonth =Joinmonth,
#Activated =Activated,
#ActivMonth =ActivMonth,
#Center = Center,
#Region = Region,
#Area= Area,
#Modem = Modem ,
#Adomstatus =Adomstatus,
#refdate = RefDate
From #tblSMS where pID = #i
Insert into TCMST5001 (CompanyCode , Address1,Address2 ,Address3 ,CreatedDate ,Status) values('001',#Add1 ,#Add2,#Add3,GETDATE(),1)
Set #count = SCOPE_IDENTITY()
Set #AddCode = 'ADD' + Cast(#count As Varchar(10))
Update TCMST5001 Set Code =#AddCode Where AddressID =#count
Set #EUCount = (Select COUNT(*) from TCCOM0005 where EnterpriseUnitCode = #Center)
if (#EUCount = 0)
Begin
Insert into TCCOM0005(AddressCode,CompanyCode,EnterpriseUnitCode,EnterpriseUnitName,Status) values(#count ,'001',#Center,#Center ,1)
END
Set #SubCount = (Select COUNT(*) from TSMST1001 where Subscriber = #Code)
if (#SubCount =0)
begin
Insert into TSMST1001(ActivationDate ,refdate , Address ,AlternateName ,Area ,Region ,Subscriber,Name ,date ,CreatedDate ,EnterpriseUnit ,Status)
values(#Activated,#refdate ,#count ,#Name,#Area,#Region,#Code,#Name ,#Joined ,GETDATE(),#Center,#Adomstatus)
end
Insert into TSMST1003 (Device ,CreatedDate ,Subscriber,StartDate) values
(#Modem,GETDATE(),#Code,#Activated)
SET #i = #i + 1
Update TSMST1005 Set Status = 0 where Jobcode = #jobCode1
Select '3' as res
END
END
Drop table #tblSMS
end
It is quite hard to give you 100% working procedure. However your problem is that you are inserting records row by row. Instead of doing that you need to insert records in BULK. That would work A LOT faster. There is rewritten procedure. You still need to rewrite 2 inserts by the same logic. Of course there could be bugs as it is fully untested. Anyway, here we are:
USE [SMS]
GO
/****** Object: StoredProcedure [dbo].[SP_ProcessFile] Script Date: 01/30/2015 12:56:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SP_ProcessFile] #JobCode VARCHAR(25)
WITH RECOMPILE
AS
BEGIN
SET NOCOUNT ON
SET #Status = ( SELECT TOP 1 status
FROM TSMST1005
WHERE Jobcode = #jobCode
)
IF ( #Status = 0 )
BEGIN
SELECT '1' AS res
END
ELSE
BEGIN
CREATE TABLE #tblSMS
(
pID INT IDENTITY(1, 1) ,
[Reference] [NVARCHAR](50) NULL ,
[Date] [VARCHAR](50) NOT NULL ,
[Code] [NVARCHAR](50) NULL ,
[Subname] [VARCHAR](500) NULL ,
[Address1] [NVARCHAR](3000) NULL ,
[Address2] [NVARCHAR](500) NULL ,
[Address3] [NVARCHAR](500) NULL ,
[Joined] [VARCHAR](50) NULL ,
[Joinmonth] [VARCHAR](50) NULL ,
[Activated] [VARCHAR](50) NULL ,
[ActivMonth] [NVARCHAR](50) NULL ,
[Center] [VARCHAR](50) NULL ,
[Region] [VARCHAR](50) NULL ,
[Area] [VARCHAR](50) NULL ,
[Modem] [VARCHAR](50) NULL ,
[Adomstatus] [VARCHAR](50) NULL ,
[RefDate] [VARCHAR](50) NOT NULL
)
INSERT INTO #tblSMS
SELECT Reference ,
[Date] ,
Code ,
Subname ,
Address1 ,
Address2 ,
Address3 ,
Joined ,
Joinmonth ,
Activated ,
ActivMonth ,
Center ,
Region ,
Area ,
Modem ,
Adomstatus ,
RefDate
FROM TSMST1005
WHERE jobcode = #jobCode1
AND Status = 1
WHILE #i <= ( SELECT COUNT(*)
FROM #tblSMS
)
BEGIN
DECLARE #minPK INT;
SELECT #minPK = MAX(AddressID ) FROM TCMST5001; -- I believe that it is identity column. If not change it to the proper one
INSERT INTO TCMST5001
( CompanyCode ,
Address1 ,
Address2 ,
Address3 ,
CreatedDate ,
Status
)
SELECT '001', Address1, Address2, Address3, GETDATE(), 1 FROM #tblSMS;
SET #AddCode = 'ADD' + CAST(#count AS VARCHAR(10))
UPDATE TCMST5001
SET Code = 'ADD' + CAST(AddressID AS VARCHAR(10))
WHERE AddressID > #minPK ;
INSERT INTO TCCOM0005
SELECT ee.cnt, t.center, t.Center, 1
FROM #tblSMS t
CROSS APPLY ( SELECT COUNT(*) AS cnt FROM TCCOM0005 e WHERE e.EnterpriseUnitCode = t.Center) ee
WHERE ee.cnt > 0
-- THE SAME LOGIC MUST BE DONE WITH THESE 2 INSERTS
SET #SubCount = ( SELECT COUNT(*)
FROM TSMST1001
WHERE Subscriber = #Code
)
IF ( #SubCount = 0 )
BEGIN
INSERT INTO TSMST1001
( ActivationDate ,
refdate ,
Address ,
AlternateName ,
Area ,
Region ,
Subscriber ,
Name ,
date ,
CreatedDate ,
EnterpriseUnit ,
Status
)
VALUES ( #Activated ,
#refdate ,
#count ,
#Name ,
#Area ,
#Region ,
#Code ,
#Name ,
#Joined ,
GETDATE() ,
#Center ,
#Adomstatus
)
END
INSERT INTO TSMST1003
( Device ,
CreatedDate ,
Subscriber ,
StartDate
)
VALUES ( #Modem ,
GETDATE() ,
#Code ,
#Activated
)
UPDATE t
FROM TSMST1005 t
SET Status = 0
JOIN #tblSMS tmp
ON tmp.jobCode1 = t.Jobcode
SELECT '3' AS res
END
END
DROP TABLE #tblSMS
END
some tips will be. Avoid doing this in every step.
SELECT COUNT(*) FROM #tblSMS
instead assign the count value to a local variable, and check the i against the same.
Also you are selecting values from these tables ( TCCOM0005, TSMST1001, TSMST1003 ) frequently , It will be good to check if these tables have proper indexes.
I have a table called customers which gets info populated from a form not all fields are required (this is because the form generates asf / xml with the inputted info) and I would like to be able to merge duplicates into one row then delete the duplicates.
Here is my table
CID | LastName | FirstName | Street | City | ZipCode | HomePhone | CellPhone | EmailAddr
1 Test NULL NULL NULL NULL NULL NULL NULL
2 NULL TEST NULL NULL NULL NULL NULL NULL
3 NULL NULL Test NULL NULL NULL NULL NULL
4 NULL NULL NULL Test NULL NULL NULL NULL
5 NULL NULL NULL NULL Test NULL NULL NULL
6 NULL NULL NULL NULL NULL Test NULL NULL
7 NULL NULL NULL NULL NULL NULL TEST NULL
8 NULL NULL NULL NULL NULL NULL NULL TEST
I want to merge the data from each field that isn't null into the Fist Instance of then update that record and delete the remaining 7 records.
I am still starting out in SQL but understand joins, inserts, updates deletes etc. Any advice or direction would be greatly appreciated. I have found multiple posts where I can merge this data in a report but not to many where I can actually truly merge the data and delete the duplicate rows.
I just found this post while searching so it may be what I am looking for
mysql-consolidate-duplicate-data-records-via-update-delete
Try this one -
SET NOCOUNT ON;
DECLARE #temp TABLE
(
CID INT PRIMARY KEY
, LastName NVARCHAR(10)
, FirstName NVARCHAR(10)
, Street NVARCHAR(10)
, City NVARCHAR(10)
, ZipCode NVARCHAR(10)
, HomePhone NVARCHAR(10)
, CellPhone NVARCHAR(10)
, EmailAddr NVARCHAR(10)
)
INSERT INTO #temp (CID, LastName, FirstName, Street, City, ZipCode, HomePhone, CellPhone, EmailAddr)
VALUES
(1, 'Test', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(2, NULL, 'TEST', NULL, NULL, NULL, NULL, NULL, NULL),
(3, NULL, NULL, 'Test', NULL, NULL, NULL, NULL, NULL),
(4, NULL, NULL, NULL, 'Test', NULL, NULL, NULL, NULL),
(5, NULL, NULL, NULL, NULL, 'Test', NULL, NULL, NULL),
(6, NULL, NULL, NULL, NULL, NULL, 'Test', NULL, NULL),
(7, NULL, NULL, NULL, NULL, NULL, NULL, 'TEST', NULL),
(8, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 'TEST'),
(12, 'Tes2', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(14, NULL, 'TES2', NULL, NULL, NULL, NULL, NULL, NULL),
(17, NULL, NULL, 'Tes2', NULL, NULL, NULL, NULL, NULL),
(18, 'Tes3', NULL, NULL, NULL, NULL, NULL, NULL, NULL),
(19, NULL, 'TES3', NULL, NULL, NULL, NULL, NULL, NULL),
(20, NULL, NULL, 'Tes3', NULL, NULL, NULL, NULL, NULL),
(21, NULL, NULL, NULL, 'Test3', NULL, NULL, NULL, NULL)
DECLARE #buffer_temp TABLE
(
CID INT PRIMARY KEY
, LastName NVARCHAR(50)
, FirstName NVARCHAR(50)
, Street NVARCHAR(50)
, City NVARCHAR(50)
, ZipCode NVARCHAR(50)
, HomePhone NVARCHAR(50)
, CellPhone NVARCHAR(50)
, EmailAddr NVARCHAR(50)
)
;WITH cte AS
(
SELECT t.CID, NextCID = ISNULL(t2.CID, (SELECT MAX(y.CID) FROM #temp y))
FROM #temp t
OUTER APPLY (
SELECT TOP 1 CID = t1.CID - 1
FROM #temp t1
WHERE t1.CID > t.CID
AND t1.LastName IS NOT NULL
) t2
WHERE t.LastName IS NOT NULL
)
INSERT INTO #buffer_temp
SELECT
t2.CID
, LastName = MAX(LastName)
, FirstName = MAX(FirstName)
, Street = MAX(Street)
, City = MAX(City)
, ZipCode = MAX(ZipCode)
, HomePhone = MAX(HomePhone)
, CellPhone = MAX(CellPhone)
, EmailAddr = MAX(EmailAddr)
FROM #temp t
CROSS APPLY (
SELECT *
FROM cte t2
WHERE t.CID BETWEEN t2.CID AND t2.NextCID
) t2
GROUP BY t2.CID
DELETE FROM #temp
INSERT INTO #temp
SELECT *
FROM #buffer_temp
SELECT *
FROM #temp
Output:
CID LastName FirstName Street City ZipCode HomePhone CellPhone EmailAddr
----------- ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
1 Test TEST Test Test Test Test TEST TEST
12 Tes2 TES2 Tes2 NULL NULL NULL NULL NULL
18 Tes3 TES3 Tes3 Test3 NULL NULL NULL NULL
It looks like you want to merge records 1-8, then 9-16, then 17-24, and so on.
Fortunately, you have a CID field that you can use for identifying the groups. All you need is the group, and the formula (CID - 1)/8 does the trick (SQL Server does integer division when dividing integers so, say, 4/8 = 0 and not 0.5). Here is the query:
select (CID - 1) / 8 as NewCID,
max(LastName) as LastName, max(FirstName) as FirstName, . . .
from t
group by (CID - 1) / 8;
I am trying to create a products table running the following SQL query:
CREATE TABLE ‘products’ (
‘id’ INT NOT NULL AUTO_INCREMENT ,
‘name’ VARCHAR( 255 ) NOT NULL ,
‘shortdesc’ VARCHAR( 255 ) NOT NULL ,
‘longdesc’ TEXT NOT NULL ,
‘thumbnail’ VARCHAR( 255 ) NOT NULL ,
‘image’ VARCHAR( 255 ) NOT NULL ,
‘sizes’ ENUM( ‘s’, ‘m’, ‘l’, ‘xl’ ) NOT NULL ,
‘colors’ ENUM( ‘red’, ‘blue’, ‘green’, ‘brown’, ‘white’, ‘black’ ) NOT NULL ,
‘grouping’ VARCHAR( 16 ) NOT NULL ,
‘status’ ENUM( ‘active’, ‘inactive’ ) NOT NULL ,
‘category_id’ INT NOT NULL ,
‘featured’ ENUM (‘true’, ‘false’) NOT NULL,
‘price’ FLOAT( 4, 2 ) NOT NULL,
PRIMARY KEY ( ‘id’ )
) TYPE = MYISAM ;
However this error occurs:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '‘s’, ‘m’, ‘l’, ‘xl’ ) NOT NULL , ‘colors’ ENUM( ‘red’, ' at line 8
Which type should I use? I am following an example from a book.
After some playing I think I can say it is the ‘’ characters that are throwing it off. I ran this without issue:
CREATE TABLE `products2` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`sizes` enum('s','m','l','xl') NOT NULL,
`colors` enum('red','blue','green','brown','white','black') NOT NULL,
`shortdesc` varchar(255) NOT NULL,
`longdesc` text NOT NULL,
`thumbnail` varchar(255) NOT NULL,
`image` varchar(255) NOT NULL,
`grouping` varchar(16) NOT NULL,
`status` enum('active','inactive') NOT NULL,
`category_id` int(11) NOT NULL,
`featured` enum('true','false') NOT NULL,
`price` float(4,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Using backticks and apostrophes instead of curly quotation marks seems to work for me
CREATE TABLE `products` (
`id` INT NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 255 ) NOT NULL ,
`shortdesc` VARCHAR( 255 ) NOT NULL ,
`longdesc` TEXT NOT NULL ,
`thumbnail` VARCHAR( 255 ) NOT NULL ,
`image` VARCHAR( 255 ) NOT NULL ,
`sizes` ENUM( 's', 'm', 'l', 'xl' ) NOT NULL ,
`colors` ENUM( 'red', 'blue', 'green', 'brown', 'white', 'black' ) NOT NULL ,
`grouping` VARCHAR( 16 ) NOT NULL ,
`status` ENUM( 'active', 'inactive' ) NOT NULL ,
`category_id` INT NOT NULL ,
`featured` ENUM ('true', 'false') NOT NULL,
`price` FLOAT( 4, 2 ) NOT NULL,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;