sql stored procedure, concat string table name with parameter value - sql

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!

Related

Storing results of a stored procedure in a Google Big Query table

Here is a link to the question I asked earlier. The accepted answer works perfectly.
But this stored procedure processes 2 statements and after running this procedure I have to click on View Results of the second statement to see the result in Google Big Query. Is there a way to save the results in some table automatically using the 'Query Settings' in Google Big Query and specifying the name of the table I want to store the results in?
You cannot set a destination table for a script (or for call a procedure), instead, you can convert your SELECT statement into CREATE TABLE ... AS SELECT, for example:
SELECT 1 x;
=>
CREATE TABLE myDataset.myTable
AS SELECT 1 x;
You can define a string parameter for your procedure. Then use this parameter in the dynamic query to write the results to the table.
CREATE OR REPLACE PROCEDURE `my_dataset.my_procedure`(destination_table STRING)
BEGIN
CREATE TEMP TABLE tmp AS SELECT 1 x;
EXECUTE IMMEDIATE (
FORMAT("CREATE OR REPLACE TABLE `%s` AS SELECT * FROM tmp", destination_table));
END
Now you can provide a table name and call this procedure to write the results to the table.
CALL `my_dataset.my_procedure`("my_dataset.my_table");
SELECT * FROM `my_dataset.my_table`

How to check if number exists in SQL

I have a stored procedure and I have to modify it so it does not accept an empty string nor a null.
I currently have this as part of the stored procedure
IF NOT EXISTS (SELECT * FROM dbo.tblUserCode WHERE iUserID = #UserID)
BEGIN
INSERT INTO dbo.tblUserCode (iUserID, sCode)
VALUES (#UserID, #Unlock)
END
However I don't want to enter anything into the table if the value is a null or string. The table only has user ids that have a code. It is currently storing 0s and empty strings when a user gets created and they leave the slot blank. I know it's this line because my access code checks if the code is 4 digits long and if it is only digits but when it returns a string that gets stored into the table.
It looks like you are using MS SQL Server (not clear from your tags) as your DBMS. In that case:
NOT ISNULL(sCode,'')=''
Will check if sCode is not Null or an empty string. Add it to your IF statement.

i want to create a stored procedure in sql that select certain rows in a table and then apply input condition on it

i want to make a store procedure or a function that select certain rows from column and then allow the user to update another column in the same table, the user should be allowed to enter the input value after the select process
alter PROC AcceptReject
#department as int,#result as bit
as
begin
select*
from Job_Seeker_apply_Job
where hr_response = 1 and department=#department
update Job_Seeker_apply_Job
set manager_response =#result
the user at the beginning should enter department number which is int value then after the selection process the user should be able to update a column called manager_response for the selected rows only

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

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.

SELECT Query selecting values based on a value in another table

I have 2 tables
Account(AccountId, Encoding)
DeviceAccountMap(AccountId, DeviceId)
Now I need to fetch the devices from the DeviceAccountMap. I pass a list of AccountId to a stored procedure and while fetching the DeviceId from the DeviceAccountMap table I need to compare the Encoding value for each account with a particular value.
Which is the easy way to do this? I am totally lost.
The select clause in the stored procedure will look something like this:
DECLARE #Accounts [usp].[Array]
and [usp].[Array] is defined as below
CREATE TYPE [usp].[Array] AS TABLE
(
Value VARCHAR(36) NULL
)
SELECT
DeviceId,
AccountEncoding = A.Encoding
FROM
usp.DeviceControllerAccountMap DCAM
INNER JOIN
usp.Account A ON (DCAM.AccountId = A.AccountId)
WHERE
DCAM.AccountId IN (SELECT Value From #AccountIds)
AND DCAM.IsShared = 1
AND AccountEncoding LIKE A.Encoding + '.%'
In other words I need to fetch the encoding value for each account and use that in this where clause.
So you can look up information on Table-Valued Parameters (TVPs) in T-SQL.
Here is an article by Erland Sommarskog.
You can refer to this StackOverflow answer to see an example of C# code calling a stored procedure that uses a TVP. I believe TVPs require SQL Server 2008 or higher.
TVPs, as far as I understand, provide a way to make your own data type in sql server that gets treated as if it was a table. You're doing this when you declare your Array type and then when you use the #AccountIds in your stored procedure's select statement.
CREATE TYPE [usp].[Array] AS TABLE -- maybe choose a more descriptive name than 'Array'
(
Value VARCHAR(36) NULL -- choose a more descriptive name than 'Value'
)
CREATE PROCEDURE [usp].[your_procedure_name]
#AccountIds [usp].[Array] READONLY -- use TVP as a parameter
AS
SELECT …
It is not clear form your question details whether you also mean to have a parameter in the stored procedure for the Encoding. It seems like you're looking for accounts whose Encodings start with a period '.'.
So first, create your type, like you're doing.
Then create your stored procedure.
Then test your stored procedure, something like this:
DECLARE #mylist Array -- make TVP sample data
INSERT #mylist(Value) VALUES(1),(11),(27),(123) -- insert some values
exec your_procedure_name #mylist -- run stored procedure
The following line is completely unnecessary. The JOIN to Account does this filter for you.
DCAM.AccountId IN (SELECT Value From #AccountIds)
Or am I missing something?