I have a stored procedure where I want to select all fields matching
the query into another table. In addition, I want to add a common
groupID to each of the records that are being inserted into the second
table.
I can get the results that I want by using a temporary table but need
to know if there is a way to do it directly..
below is the code that uses the temporary table..
CREATE TABLE #tempStore_DeliveryAddress (
[AddressId] [int] IDENTITY (1, 1) NOT NULL ,
[UserId] [int] NOT NULL ,
[Title] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
,
[FirstName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[SpouseName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[MiddleName] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[SpouseMiddleName] [varchar] (10) COLLATE
SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[LastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[SpouseLastName] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL ,
[Suffix] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[SpouseSuffix] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Company] [varchar] (64) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Address1] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Address2] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Address3] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[City] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[State] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
,
[PostalCode] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[Country] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[ForeignFlag] [int] NULL CONSTRAINT
[DF_Store_DeliveryAddress_ForeignFlag] DEFAULT (0),
[email] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
,
[Greeting] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[FullName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[ShortName] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
[OptOut] [int] NULL CONSTRAINT [DF_Store_DeliveryAddress_OptOut]
DEFAULT (0),
[Modified] [datetime] NULL CONSTRAINT
[DF_Store_DeliveryAddress_Modified] DEFAULT (getdate()),
[Modifer] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_Store_DeliveryAddress_Modifer] DEFAULT ('DBA'),
[Created] [datetime] NULL CONSTRAINT
[DF_Store_DeliveryAddress_Created] DEFAULT (getdate()),
[Creator] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
CONSTRAINT [DF_Store_DeliveryAddress_Creator] DEFAULT ('DBA'),
[MailListID] [int] NULL ,
CONSTRAINT [PK_Store_DeliveryAddress] PRIMARY KEY CLUSTERED
(
[AddressId]
) ON [PRIMARY]
) ON [PRIMARY]
INSERT INTO #tempStore_DeliveryAddress([UserId], [Title], [FirstName],
[SpouseName], [MiddleName], [SpouseMiddleName], [LastName],
[SpouseLastName], [Suffix], [SpouseSuffix], [Company], [Address1],
[Address2], [Address3], [City], [State], [PostalCode], [Country],
[ForeignFlag], [email], [Greeting], [FullName], [ShortName], [OptOut],
[Modified], [Modifer], [Created], [Creator])
(SELECT [UserId], [Title], [FirstName], [SpouseName], [MiddleName],
[SpouseMiddleName], [LastName], [SpouseLastName], [Suffix],
[SpouseSuffix], [Company], [Address1], [Address2], [Address3], [City],
[State], [PostalCode], [Country], [ForeignFlag], [email], [Greeting],
[FullName], [ShortName], [OptOut], [Modified], [Modifer], [Created],
[Creator]
FROM [ntmportal].[dbo].[Store_AddressBook]
WHERE [AddressID] in (Select AddressID From Store_AddressesForGroup
where AddressGroupID = 322))
UPDATE #tempStore_DeliveryAddress set MailLISTID = 422
INSERT INTO Store_DeliveryAddress([UserId], [Title], [FirstName],
[SpouseName], [MiddleName], [SpouseMiddleName], [LastName],
[SpouseLastName], [Suffix], [SpouseSuffix], [Company], [Address1],
[Address2], [Address3], [City], [State], [PostalCode], [Country],
[ForeignFlag], [email], [Greeting], [FullName], [ShortName], [OptOut],
[Modified], [Modifer], [Created], [Creator], [MailListID])
(Select [UserId], [Title], [FirstName], [SpouseName], [MiddleName],
[SpouseMiddleName], [LastName], [SpouseLastName], [Suffix],
[SpouseSuffix], [Company], [Address1], [Address2], [Address3], [City],
[State], [PostalCode], [Country], [ForeignFlag], [email], [Greeting],
[FullName], [ShortName], [OptOut], [Modified], [Modifer], [Created],
[Creator], [MailListID]
FROM #tempStore_DeliveryAddress) 2 2473
INSERT INTO Store_DeliveryAddress
(userid, title, firstname, spousename, middlename, spousemiddlename,
lastname, spouselastname, suffix, spousesuffix, company, address1,
address2, address3, city, state, postalcode, country, foreignflag,
email, greeting, fullname, shortname, optout, modified, modifer,
created, creator, maillistid)
SELECT userid, title, firstname, spousename, middlename, spousemiddlename,
lastname, spouselastname, suffix, spousesuffix, company, address1,
address2, address3, city, state, postalcode, country, foreignflag,
email, greeting, fullname, shortname, optout, modified, modifer,
created, creator, 422
FROM Store_AddressBook
WHERE addressid IN
(SELECT addressid
FROM Store_AddressesForGroup
WHERE addressgroupid = 322)
Do you really need to duplicate the data in a second table
(Store_DeliveryAddress)? Maybe you could just create a view based on the
above SELECT statement.
--
David Portas
SQL Server MVP
--
You can use a qualified asterisk. Note that asterisk is often frowned
upon as bad style.
INSERT INTO Store_DeliveryAddress
(userid, title, firstname, spousename, middlename, spousemiddlename,
lastname, spouselastname, suffix, spousesuffix, company, address1,
address2, address3, city, state, postalcode, country, foreignflag,
email, greeting, fullname, shortname, optout, modified, modifer,
created, creator, maillistid)
SELECT Store_AddressBook.*, 422
FROM Store_AddressBook
WHERE addressid IN
(SELECT addressid
FROM Store_AddressesForGroup
WHERE addressgroupid = 322)
Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Marko Poutiainen |
last post by:
Situation:
We had to make our SQLServer 2000 database multi-lingual. That is, certain
things (such as product names) in the database should be shown in the
language the user is using (Finnish,...
|
by: Joachim Klassen |
last post by:
Hi all,
first apologies if this question looks the same as another one I recently
posted - its a different thing but for the same szenario:-).
We are having performance problems when...
|
by: jjturon |
last post by:
Can anyone help me??
I am trying to pass a Select Query variable to a table using Dlookup
and return the value to same select query but to another field.
Ex.
SalesManID ...
|
by: ing42 |
last post by:
I have a problem with inserting records into table when an indexed view
is based on it.
Table has text field (without it there is no problem, but I need it).
Here is a sample code:
USE test
GO...
|
by: clinttoris |
last post by:
Hello,
If someone could help me it would be appreciated as I am not having
much luck.
I'm struggling with my asp code and have some questions relating to asp
and oracle database.
First...
| |
by: Andy C Matthews |
last post by:
Hi there,
I'm building an Access database and using VBA to generate Microsoft Word
mailings for customers. It's all going fine so far. However, a variables
named ParcelID, a ten-digit string such...
|
by: c676228 |
last post by:
Hi everyone,
I am just wodering in asp program, if there is anybody writing store
procedure for inserting data into database since there are so many parameters
need to be passed into store...
|
by: sdanda |
last post by:
Hi ,
Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity .........
This has to work for more than 8,00,000...
|
by: rando1000 |
last post by:
Okay, here's my situation. I need to loop through a file, inserting records based on a number field (in order) and if the character in a certain field = "##", I need to insert a blank record.
...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |