|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today! |
|
#1
|
|||
|
|||
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 ". |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
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é |
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Plz help!! I m trying to describe question more clearly |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|