SunQuest
           MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old July 8th, 2004, 08:58 AM
syedshan syedshan is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 3 syedshan User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Plz help!! I m trying to describe question more clearly

hi @ll...

Plz Plz help me out.!!!!!!!!

my query is that in my database i've a table say Tb1 and another table called Tb2.

in TB1 there is a field called companyID (the data is for e.g "001")

similarly in TB2 there is a filed called productID (the data is for example "01")

now how can i create a field in another table say TB3 with a field called UserID and the data should be like the companyID from TB1 + productID from Tb2. The data should come like this for example " 001-01 ".

Reply With Quote
  #2  
Old July 8th, 2004, 10:46 AM
pickledshark pickledshark is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Blackpool
Posts: 22 pickledshark User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 m 32 sec
Reputation Power: 0
Send a message via ICQ to pickledshark Send a message via MSN to pickledshark
To clarify, for each combination of companyid + '-' + productid you want to insert a new column in another table?

If you wish to create/ammend a table based upon dynamic data, you need to use dynamic sql. Example:

DECLARE @tablename VarChar(100)
DECLARE @FieldName VarChar(100)
DECLARE @sql VarChar(1000)

SET @tablename = 'monkeys'
SET @FieldName = 'monkeyID'

SET @sql = 'CREATE TABLE '+@tablename+'('+@FieldName+' Int )'

EXEC (@sql)
SELECT * FROM monkeys
DROP TABLE monkeys

-----------------------------------------------------

Hope this helps

Reply With Quote
  #3  
Old August 25th, 2004, 04:12 PM
Blase Blase is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Posts: 11 Blase User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To rephrase your question, you wish to concatenate the data from two different fields into a third field.

Try a variation of the following query:
SELECT UserID = TB1.CompanyID + ' - ' + TB2.ProductID
FROM TB1
INNER JOIN TB2 ON
TB1.<Field3> = TB2.<Field3> [AND TB1.<FieldN> = TB2.<FieldN> AND...]
[WHERE ...]


If you're confused about all the place-holders above, copy-and-paste this into a new query and execute it (WITHOUT the Drop Tables at the end, first, to see the results.)
IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = object_id(N'[dbo].[TB1]') AND OBJECTPROPERTY(ID, N'IsUserTable') = 1) DROP TABLE [dbo].[TB1]
GO
CREATE TABLE TB1 (TB1Key int, CompanyID nvarchar(10))
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = object_id(N'[dbo].[TB2]') AND OBJECTPROPERTY(ID, N'IsUserTable') = 1) DROP TABLE [dbo].[TB2]
GO
CREATE TABLE TB2 (TB2Key int, ProductID nvarchar(10))
GO

IF EXISTS (SELECT * FROM dbo.sysobjects WHERE ID = object_id(N'[dbo].[TB3]') AND OBJECTPROPERTY(ID, N'IsUserTable') = 1) DROP TABLE [dbo].[TB3]
GO
CREATE TABLE TB3 (TB3Key int, UserID nvarchar(20))

INSERT INTO TB1 (TB1Key, CompanyID) VALUES (1001, '001')
GO
INSERT INTO TB2 (TB2Key, ProductID) VALUES (1001, '01')
GO
INSERT INTO TB3 (TB3Key, UserID)
SELECT
TB1.TB1Key,
UserID = TB1.CompanyID + ' - ' + TB2.ProductID
FROM TB1 INNER JOIN TB2 ON TB1.TB1Key = TB2.TB2Key

GO

-- DROP TABLE [dbo].[TB1]
-- GO

-- DROP TABLE [dbo].[TB2]
-- GO

-- DROP TABLE [dbo].[TB3]
-- GO


-Blasé

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Plz help!! I m trying to describe question more clearly


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway