Aniket Narvankar

Aniket Narvankar

  • 537
  • 2.1k
  • 581.6k

Sql Query

Mar 21 2016 3:05 AM
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. 
 

Answers (4)