0

I have an SQL query in which I need to insert a string variable but I am getting errors from it.

Below is my SQL INSERT query:

Insert into User(Employee, Name, Address, Role, Action) 
Select distinct ur1.Employee, ur1.Name, ur1.Address, ur1.Role, ['%" + action + "%'] as [Action] FROM UserRoles ur1)";

string action="insert delete query in database"

I would like to insert string action into my INSERT statement, but I am getting lots of syntax errors.

Can anyone help me with the query?

Thank you in advance for any help.

2
  • Show us the errors please. And maybe you also need to use FROM UserRoles ur1) if you don't want engine complains because ur1.Employee, ... don't exist. Commented Dec 1, 2011 at 11:47
  • Do you want the sql for this, if so which DBMS (Sql Server, ACcess Oracle etc), or do you want cosde, if so what language. Commented Dec 1, 2011 at 11:48

4 Answers 4

2

The SQL operator for string concatenation is ||.

Sign up to request clarification or add additional context in comments.

2 Comments

hi CodeBrikie, what do you mean by string concatenation? What do I have to replace with |?
@RUiHAO "adding strings". You should replace "+" by "||".
1
Insert into User(Employee, Name, Address, Role, Action) 
Select distinct ur1.Employee, ur1.Name, ur1.Address, ur1.Role,'insert delete query in database' 
FROM UserRoles ur1;

2 Comments

ur1.Employee,... doesn't exist check out the comment of @Marco
As I've already told in my post comment, ur1 is not defined ;)
1

You should try

INSERT INTO User (Employee, Name, Address, Role, Action) 
SELECT DISTINCT ur1.Employee, ur1.Name, 
                ur1.Address, ur1.Role, 
                ['%insert delete query in database%'] 
FROM UserRoles ur1

Comments

1

The part where you are concatenating your action variable to the rest of the script looks something like this after the concatenation:

…
  ['the string value goes here'] as [Action]
…

I'm now going to take a wild guess but it does seem as if you are on SQL Server or on Sybase, because of the square brackets. Now, in either of those two products, the sequences of characters enclosed in square brackets would be considered delimited identifiers. Using the brackets in your example does make sense for [Action] but seems absolutely pointless for the ['...'] part of your script. It is more likely that you meant simply '...', i.e. without the square brackets, which simply stands for a string constant.

One other thing to note: there's an unmatched closing parenthesis at the end of your SQL statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.