Slay29 Slay29 - 1 month ago 8
MySQL Question

Using value from another table in insert

I have really annoying problem. I have this query that I am sending from c# project. Problem is that I cannot succeed to use value from another table into my insert. Here is my sample of code:

INSERT Izdavanje (registracioniBrojKorisnika, imeKorisnika, identifikacioniBrojKnjige, vremeidatumIzdavanja)
VALUES (@rbKorisnika,
(SELECT imeKorisnika
FROM Korisnici
WHERE registracioniBrojKorisnika=@rbKorisnika),
@idKnjige,
GetDate());

Answer

Just drop the VALUES clause:

INSERT INTO Izdavanje (registracioniBrojKorisnika, imeKorisnika, identifikacioniBrojKnjige, vremeidatumIzdavanja) 
  SELECT @rbKorisnika, imeKorisnika, @idKnjige, GetDate()
  FROM Korisnici 
  WHERE registracioniBrojKorisnika=@rbKorisnika