I have tables in database from TableA to TableZ that is 26 Tables.
I was asked a question in an interview if Suppose a string comes as 'Abcdef' then its first character is A A so it should be inserted into TableA,if Suppose first character is B then it should be inserted into TableB
I wrote it like this
Declare @Name varchar(50),@var varchar(50),@tbl varchar(50)
Set @Name = 'ABCDEFG'
Set @var = Substring(@Name,1,1)
if(@var = 'a')
insert into tblA (Name) values (@Name)
but this is the wrong way the interviewer told me there will be a need to write 26 times if else.Please do let me know how to solve the above question as I am stuck on it.


Aniket NarvankarRank #903 · 2.3K reputationPosted Mar 21, 2016, 2:23 AM EDT
Guest UserRank #816 · 2.8K reputationPosted Mar 20, 2016, 11:56 PM EDT
Declare @Name varchar(50),@var varchar(50),@tbl varchar(50)Set @Name = 'ABCDEFG'Set @var = Substring(@Name,1,1)Set @Sql = 'insert into tbl' + @var + ' (Name) values (@Name)'EXECUTE sp_executesql @SqlAniket NarvankarRank #903 · 2.3K reputationPosted Mar 21, 2016, 1:09 AM EDT
Guest UserRank #816 · 2.8K reputationPosted Mar 21, 2016, 2:08 AM EDT