Appending character to variable in execute query - sql

I want to execute a dynamic SQL statement, which searches for names whose last name is always a constant and first name is a variable.
Here is a query I have written for selecting a row with name='Test lastname'.
EXECUTE 'SELECT name FROM users
WHERE name=$1 lastname'
USING ('Test');
This generates a syntax error. Is it possible to do this?

I think you need something like this:
EXECUTE 'SELECT user_id FROM users
WHERE name=$1'
USING Test||' lastname' ;
Here Test is variable and 'lastname' is hard coded value
Also another way is as #JorgeCampos mentioned:
...WHERE name=$1 || '' lastname''' USING 'Test';

Related

SSIS Execute SQL Task contain Insert Into and Select

I am attempting to read file names then input each name in a row with another similar cell value to another table column. Within the For Each Loop, then I have the Execute SQL Task. currently the Task is set to Direct Input. This is the problem script
INSERT INTO TableB
(LoopValue, Columnvalue)
VALUES (?, N'Select Columnvalue from TableA where Columnvalue like ?')
You have a for each loop over files, as I understood it? When I have these kind of situations I simply map the current file to a fileName variable using the "Variable Mapping" setting of the foreach loop, using index 0.
Then I create a Data Flow task with a derived column which is simply your fileName variable. Using this in conjunction with an OLE DB destination ought to be sufficient to insert the value into a table, if I understand you correct?
EDIT:
Oh you want to create a string as well? You could achieve that in your Control Flow using an Expression Task. Assign a string variable, such as "queryString" and then have:
#queryString = "Select " + #[User::fileName] + " from Table B"
or something in the lines of that, i.e. use "" to insert "hard coded" values and a + followed by your variable to concatenate your variable. You could then also use a derived column for your #queryString variable in your data flow as well.
You're close if I understand what you are attempting to do which is insert into TableB selecting records from TableA where ColumnValue is like the passed in value.
The way you have it now it wants to insert the literal string of Select Columnvalue from TableA where Columnvalue like ? into your table.
Tweak your statement like this:
INSERT INTO TableB (LoopValue, Columnvalue)
SELECT ?, Columnvalue FROM TableA WHERE Columnvalue LIKE '%' + ? + '%' --% is a wildcard. If you only want those that begin or end then remove one or the other %
Then make sure you're passing in the value twice. Since there are multiple ? that relates to the number of parameters, but you can pass it twice since you are using the same value in two different places:
Parameter names would then start with 0 and increment up to match the ? in your statement. Depending on your connection type that could be different, there is a section in the following documentation that tells you what it should be Execute SQL Task

Using Like '%#mail%' in stored procedure

If I try to call a stored procedure to check for mail addresses containing a #mail it fails because it sees the #mail as a variable instead of a string literal
Is this what you want?
where email like '%' || #mail || '%'
|| here is the standard operator for string concatenation. This can very depending on the database.
You can use
Concat function
SELECT * FROM TableA WHERE Email LIKE CONCAT('%',#Email,'%')
I ended up creating a variable which holds the like #domainname placed it in the exec statement and it started working. So I guess the variable gets parced, the value is added to the query and its accepted as text in stead of searching for a variable

Like function in parameter stored procedure

Good day
I have problem with my store procedure as follow:
I need to know wchich manager have which employee
My statement with function WITH as SELECT working fine ,but
my problem is with procedure when iam caling back Iam geting 0 rows back.
I think should by problem with Function LIKE when Iam creating procerure.
Thank you for opinions
create procedure test #Name varchar(200) as
WITH n(Surname,Name) AS
(SELECT Surname,Name
FROM [dbo].[phonebook]
WHERE Name like '%#Name' --I need to be like here beause before name is XT245*/ SUBCODE
UNION ALL
SELECT subs.Surname, subs.Name
FROM [dbo].[phonebook] as subs, n
WHERE n.Surname = subs.Boss)
SELECT Name FROM n
exec test 'XX37485*/John'
This expression:
WHERE Name like '%#Name' --I need to be like here because before name is XT245*/ SUBCODE
is looking for names that have exactly that pattern of characters . . . # N a m e. There is no variable substitution.
If you want to use like, then:
WHERE Name like '%' + #Name
Note that this requires that the pattern end with #Name, so it will not match #Name anywhere in the string.

Trouble Getting Columns Names to Variable in SSIS Execute SQL Task

I'm attempting to validate some column headings before the import of a monthly data set. I've set up an Execute SQL Task that's supposed to retrieve the column headings of the prior month's table and store it in Header_Row as a single string with the field names separated by commas. The query runs just fine in SQL Server, but when running in SSIS, it throws the following error:
"The type of the value (Empty) being assigned to variable 'User:Header_Row' differs from the current variable type (String)."
1) Does this mean that I'm not getting anything back from my query?
2) Is there another method I should be using in SSIS to get the query results I'm looking for?
3) Is there an issue with me using the variable reference in my query as a portion of a string? I think the answer is yes, but would like to confirm, as my variable was still empty after changing this.
Original Query:
SELECT DISTINCT
STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = 'dt_table_?'
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
EDIT: After changing the variable to cover the full table name, I have a new error saying "The value type (__ComObject) can only be converted to variables of the type Object."
Final Query:
SELECT DISTINCT
CAST(STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = ?
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') As varchar(8000)) AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
You are attempting to parameterize your query. Proper query parameterization is useful for avoiding SQL Injection attacks and the like.
Your query is looking for a TABLE_NAME that is literally 'dt_table_?' That's probably not what you want.
For laziness, I'd just rewrite it as
DECLARE #tname sysname = 'dt_table_' + ?;
SELECT DISTINCT
STUFF((
SELECT
',' + COLUMN_NAME
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS aa
WHERE
TABLE_NAME = #tname
ORDER BY
aa.ORDINAL_POSITION
FOR
XML PATH('')
), 1, 1, '') AS Fields
FROM
db_Analytics.INFORMATION_SCHEMA.COLUMNS a;
If that's not working, you might need to use an Expression to build out the query.
I'm really pretty sure that this is your problem:
TABLE_NAME = 'dt_table_?'
I'm guessing this is an attempt to parameterize the query, but having the question mark inside the single-quote will cause the question mark to be taken literally.
Try like this instead:
TABLE_NAME = ?
And when you populate the variable that you use as the parameter value, include the 'dt_table_' part in the value of the variable.
EDIT:
Also in your ResultSet assignment, try changing "Fields" to "0" in the Result Name column.
There are two issues with the query above:
1) The query in the task was not properly parameterized. I fixed this by putting the full name of the prior month's table into the variable.
2) The default length of the result was MAX, which was causing an issue when SSIS would try to put it into my variable, Header_Row. I fixed this by casting the result of the query as varchar(8000).
Thanks for the help everyone.

How to use parameters in contains method in store procedure

I am testing contains method in store procedure. I would like to see data result like:
Example Result for John:
John Nick
Papa John
Harly John Fredy
I don't want to use LIKE method. Can we use starts with or ends with (*) operator with parameters in SP?
CONTAINS(name,#name) // query is working
but if i can try like this:
CONTAINS(name, '"john" OR "john*"') // query is working
CONTAINS(name,'"#name" OR "#name*"') // query is not working
With parameters same query is not working in SP. Is it posibble to do this in SP?
Thanks,
With CONTAINS, in order to pass a wildcard through with a variable, you need to make the wildcard a part of the variable.
The easiest way to do it within a stored procedure is adding a variable (or modifying your current variable).
For example,
DECLARE #nameX NVARCHAR(4000) = '"' + #name + '" OR "' + #name + '*"'
...
CONTAINS(name, #nameX)