I tried to add a new column to a table in same db, I then tried to copy new column from another table’s column.
ALTER TABLE [library].[dbo].[Member$]
add new_column1 float
go
update [library] .[dbo] .[Member$]
set new_column1 = select [library].[dbo].Category$ .Category from [library] .[dbo] .[Category$]
GO
CREATE TABLE [dbo].[Category$](
[CatID] [float] NULL,
[Category] [nvarchar](255) NULL,
[BooksLimit] [float] NULL,
[Period] [float] NULL,
[FinePerDay] [float] NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Member$](
[Roll No] [float] NULL,
[RollNo] [nvarchar](255) NULL,
[Name] [nvarchar](255) NULL,
[CatID] [nvarchar](255) NULL,
[ExpiryDate] [datetime] NULL,
[ExpiryReason] [nvarchar](255) NULL,
[Session] [nvarchar](255) NULL,
[new_column] [float] NULL,
[new_column1] [float] NULL
) ON [PRIMARY]
But the new_column1 is still null
There are multiple values in Category, but row number in both doesnot match, Is that a problem, If yes then how can I achieve this?
Try this:
BUT
Doing so you just assigns the same value to all the rows in table Member$, if you need specific settings for each Member$’s row – try to place proper filtering in the subquery
like this:
BUT, special note
If you want to use ID for category – better use for it an exact type, such as
int– becauseFLOATtypes family are not exact and thus – harder to compare.