SQL - stored procedure, pass field and table name parameters on execute - sql

I created a SQL procedure that replaces all values in a field with Xs the same length as the original values.
Update Table
Set Name = Replicate('x', Len(Name))
I am trying to alter this procedure so that I can just pass a table and field name as a parameter on execute instead of editing the stored procedure every time I want to pass a new field. Is there a way to do this?
This is what I think the execute statements should look like when I want to x out the values in another field:
Exec MyProcedure ‘Users’, ‘Email’

You can use EXEC to execute sql statements like this: EXEC (#sqlCommand). SqlCommand would be the string composed by you based on the received parameters.
Also, another option would be to run the statement using sp_executesql.

Related

sql stored procedure, concat string table name with parameter value

i have created a stored procedure that contains a parameter. I want to use this parameter as part of the table name. there are 2 tables a user can choose
by entering the final 4 values
[Summary_tableStore4302]
[Summary_tableStore4304]
i want the numericalportion as the parameter. This will be executed in Tableau, so user can enter either 4302 or 4304, the select statement will grab data from this table.
the trouble I am having (i think) is trying to concat the string portion of the table name to the parameter value and using it within the select statement
CREATE PROCEDURE testTableau4
#BM1 int = 0
Set #tableName = concat('[Summary_tableStore',#BM1,']' );
select * from #tableName as t1
End
Go
what is the proper way to concat a string value and integer parameter value and use it as a table name within Select statement, specifically within a Stored Procedure. thank you!

Update from stored procedure return

I have a stored procedure that I want to run on every row in a table that matches a where clause, the procedure already exists on the server and is used in other places so it cannot be modified for these changes.
The stored procedure returns a scalar value, I need to store this value in a column in the table, I've tried using the update:
UPDATE tbl SET tbl.Quantity =
EXEC checkQuantity #ProductID = tbl.ProductID, #Quantity = tbl.Quantity
FROM orders tbl WHERE orderNumber = #orderNumber
But this of course doesn't work, is there a way to do this without multiple queries, reading the line info, running the proc in a loop then updating the original line?
No there is no way to do this without multiple queries. This is one of the few scenarios where a cursor or loop is necessary.
Unless you can replace your stored procedure with a user-defined function, which can be run in the context of a single query.

writing a EXECUTE statement in tOracleRow component using context variable

I am trying to write an EXECUTE statement in toraclerow.
At each iteration the string is prepared dynamically from the flow. Here I am not discussing how I have prepared the string. But once prepared the entire string is stored in a single context variable. For example I have the following string stored in a context variable at a given iteration number.
context.FinalString = "Insert into TargetTableName (columnA, columnB)
SELECT Col_A, Col_B
FROM SourceTableName"
I am trying to execute this string in the tOracleRow component using the following statement:
"EXEC SQL EXECUTE '"+context.FinalString+"'"
On running the job I am getting the following error.
ORA-00900: invalid SQL statement
Kindly suggest a solution. Is there a way to execute a sql statement stored as a string in a context variable?
try to execute by removing text EXEC SQL EXECUTE ..just simply the text should be the statement - insert into table select col from table...
or
update table set column=value where ...
you dont need exec sql execute text here.

Is it possible to retrieve Selected Columns from Stored Procedure?

I have a stored procedure which returns a few columns from a SELECT. Now I need to grab 2 columns out of those columns in my new stored procedure and use them.. I am trying to do this using EXEC method. Is it possible to do this?
Ex : Original stored procedure:
CREATE PROCEDURE myBaseProcedure
#stId INT
AS
BEGIN
SELECT Name,
Address,
StudentId,
Grade
FROM Student
WHERE StudentId = #stId
END
New stored procedure:
CREATE PROCEDURE myNextProcedure
BEGIN
EXEC myBaseProcedure 19 -- Here I need to grab only StudentId and Name??
END
Given that you cannot dump to a temp table or table variable since the base stored procedure might sometimes add columns, there are three approaches that would do this:
You can effectively SELECT from a stored procedure using either OPENROWSET or OPENQUERY
You can use SQLCLR to create a table-valued function that executes the procedure, returns a struct of just the fields that you want, which will be the only fields that you read or "get" from the SqlDataReader.
You can use SQLCLR to create a stored procedure that executes the procedure to get a SqlDataReader, and instead of returning the SqlDataReader to SqlContext.Pipe.Send(), you would use SendResultsStart, SendResultsRow, and SendResultsEnd. You would create a SqlDataRecord of just the fields you wanted, and those would also be the only fields that you read or "get" from the SqlDataReader. While this still leaves you with a stored procedure, the filtering of the fields is done within the CLR-based proc so the output is guaranteed to be just the fields you want, regardless of how the result set structure of the base stored procedure changes. In this way you could create a local temp table to dump the results to, which would be better for JOINing to other tables. This method also allows for you to pass in a list of fields to the CLR-based stored procedure that would be parsed and used as the fields to dynamically construct the SqlDataRecord with as well as to dynamically determine which fields to get from the SqlDataReader. That would be a little more complicated but also quite a bit more flexible :).
You don't need to create a new stored procedure for this, you can integrate the stored proc call in a simple query using OpenQuery or use a temporary table.
Using OPENQUERY
SELECT Name,
Address
FROM OPENQUERY(ServerName, 'EXEC myBaseProcedure 19')
-- WHERE your_field = expected_value --> if you need to add filters
Using Temp table
Declare #MyTempTable Table (columns definitions)
Insert #MyTempTable Exec myBaseProcedure 19
Select Name,
Address
FROM #MyTempTable

'Execute Immediate' with Into Clause in HANA

I have a requirement where-in I need to read a table (table name provided as input parameter of the SP), store the results in a temp table and then store the count of the read table into a variable. Please advise how can this be achieved. I have been able to read the table and its count using dynamic query but am not able to put the results in a temp table/ variable. 'Select' and 'Into' clauses do not seem to be working with 'Execute Immediate'. Thanks.
It is not very clear to me exactly what is being asked, but you should be able to execute a SELECT statement in the following manner:
CREATE PROCEDURE p1(IN tablename VARCHAR) AS
BEGIN
execute immediate 'SELECT * FROM ' || :tablename;
END;
Then the following statements create a table and call the procedure to retrieve the result:
create table T (i integer);
insert into T values (123);
The following would produce a result set with one row/column with the value 123:
CALL p1('T')
Please note that with this type of functionality, you need to be very careful not to allow any user-provided input to be given directly to a procedure that uses EXECUTE IMMEDIATE to avoid the possibility of SQL injection attacks.