I have the following SQL query in stored procedure and trying to call Table-value function(fn_get_type_ids). I am getting 'Must declare the scalar variable "#category_id".' error message. Table-value function returns muliple IDs. How do I call this function?
The stored procedure works if I pass hard coded values to the function.
dbo.fn_get_type_ids(2, 90, NULL). It doesn't work if I pass variables shown below. What could be wrong? please suggest.
ALTER PROCEDURE [dbo].[get_search_results]
#user_id BIGINT,
#category_id INT = NULL,
#a_id INT = NULL,
#all_detail_id INT = NULL
AS
BEGIN
DECLARE #select_list VARCHAR(4000)
DECLARE #where_condition VARCHAR(4000)
SELECT #select_list = 'SELECT DISTINCT c.table1_id, c.table1_number, c.type_id '
SELECT #select_list = #select_list + ' FROM dbo.TABLE1 c '
SELECT #select_list = #select_list + ' LEFT JOIN TABLE2 cb ON cb.table1_id = c.table2_id '
SELECT #where_condition = ' WHERE c.active_flag = 1'
SELECT #where_condition = #where_condition + ' AND c.type_id in
(select type_id from dbo.fn_get_type_ids(#category_id, #a_id, #all_detail_id)) '
END
You are using dynamic sql and sql variables exist in different sessions, to make it work you need to change it to:
SELECT #where_condition = #where_condition + ' AND c.type_id in
(select type_id from dbo.fn_get_type_ids('+CAST(#category_id as varchar))+',
'+CAST(#a_id as varchar))+', '+CAST(#all_detail_id as varchar))+')) '
Try making that last select:
SELECT #where_condition = #where_condition + ' AND c.type_id in
(select type_id from dbo.fn_get_type_ids(' + ISNULL(#category_id,'NULL') + ', ' + ISNULL(#a_id,'NULL') + ', ' + ISNULL(#all_detail_id,'NULL') + ')) '
Related
I'm working with Dynamic SQL (still in the learning phase) and I'm stuck at a part where I need to use a WHILE loop:
SET #tableName = (SELECT DISTINCT TableName FROM #dataStructure)
Here basically I want to make sure that the operations inside the while loop should occur for all the tables in the #tableName (defined above). I don't know how I can give this condition as an input for the while loop.
WHILE() #HOW CAN I PUT THE CONDITION HERE????
BEGIN
SET #str = ''
SET #sqlstr = ''
SELECT #table = TableName FROM #dataStructure
SET #str = 'UPDATE a0' + char(13) + char(10)
+ ' SET a0.Mjolnir_Source_ID = CONCAT( '
SELECT #str = #str + IIF(ReferenceTable IS NULL, 'a0.' + columnName , alias + '.Mjolnir_Source_ID') + ','
FROM #dataStructure
WHERE TableName = #tableName AND ReferenceTable IS NOT NULL
ORDER BY columnName
SELECT #str = #str + ') FROM ' + #table + ' a0'
SELECT #sqlstr = #sqlstr + +
+ ' INNER JOIN ' + QUOTENAME(#U4SM_db_name) + '.dbo.' + QUOTENAME(ReferenceTable) + ' ' + alias + char(13) + char(10)
+ ' ON a0.' + columnName + ' = ' + alias + '.' + ReferenceColumn + char(13) + char(10)
FROM #dataStructure
WHERE TableName = #tableName AND ReferenceTable IS NOT NULL
ORDER BY columnPosition
select #str + #sqlstr
select #sqlstr
SET #tableName = #tableName + 1
END
Can anyone please help me out here?
Here's an example of a WHILE loop. Basically, you get the first TableName, then if it's NOT NULL, you do your functions. Then get the next table name, and repeat as necessary.
DECLARE #CurrentTableName nvarchar(100)
DECLARE #CustomSQL nvarchar(4000)
SET #CurrentTableName = (SELECT TOP 1 TableName FROM #dataStructure ORDER BY TableName)
WHILE #CurrentTableName IS NOT NULL
BEGIN
SET #CustomSQL = 'SELECT TOP 10 * FROM ' + #CurrentTableName
EXEC (#CustomSQL)
SET #CurrentTableName = (SELECT TOP 1 TableName FROM #dataStructure WHERE TableName > #CurrentTableName ORDER BY TableName)
END
Note that SQL commands often cannot contain variable names in key spots (e.g., SELECT * FROM #tableName). Instead, you save it as an SQL string (what I've called #CustomSQL above) and then EXEC it (put brackets around the variable name though).
Edit: Do this on a test site first before production, and know where the 'cancel query' button is. It's not often, but it's also not unknown, that the 'getting the next row' part isn't properly written and it just runs in a perpetual loop.
FETCH CURSOR with WHILE. Example:
DECLARE myCursor CURSOR FOR
SELECT DISTINCT TableName FROM #dataStructure;
OPEN myCursor;
FETCH NEXT FROM myCursor INTO #table:Name;
WHILE ##FETCH_STATUS = 0
BEGIN
Print ' ' + #TableName
FETCH NEXT FROM myCursor INTO #TableName;
END;
CLOSE myCursor;
DEALLOCATE myCursor;
GO
Don't recreate the wheel unless you need a better wheel:
sp_MSforeachtable
https://www.sqlshack.com/an-introduction-to-sp_msforeachtable-run-commands-iteratively-through-all-tables-in-a-database/
If you are worried about using an undocumented procedure in production that might change in the future, simply script it out and create your own custom named version.
I am developing a custom application using CodeIgniter and MSSQL Server. Here i am using stored procedures.
Now i am wondering to implement codeigniter query type functionality where i can create a universal stored procedure in SQL Server and at the time of using i can pass tablename, array of fields and values.
It can work for both insert and update.
Something like we do in CodeIgniter to execute the query,
$data = array('fieldname1' => 'value1',
'fieldname2' => 'value2');
$this->db->insert($tablename,$data);
Just like this if we can pass the table name and array of the data to stored procedure and stored procedure automatically execute it.
If this can be done, it can save lots n lots of man hours. If anyone have already done i will be very much happy to see the solution.
You need to make string very specific in this case.
Figure out your table name, Column name, Column values for insert. For update 2 more parameters are required Id column name and its value.
GO
---- exec InsertUpdate 'tablename', 'col1, col2, col3', 'val1, val2, val3', 'idcol', 'idval'
GO
Create proc InsertUpdate
( #TableName nvarchar(500),
#ColName nvarchar(max),
#ColValues nvarchar(max),
#IDColName nvarchar(100) = '', --- for update only otherwise null
#IdColValue nvarchar(Max) = '' --- for update only otherwise null
)
As
Begin
declare #Query nvarchar(max)
if (#IdColValue = '')
Begin
set #Query = ' Insert into ' + #TableName + ' (' + #ColName + ') values (' + #ColValues + ')'
End
Else
Begin
;with CtColumn as (
select ROW_NUMBER() over (order by (select 1000)) as Slno, * from Split(#ColName,',') )
, CtValue as (
select ROW_NUMBER() over (order by (select 1000)) as Slno, * from Split(#ColValues, ','))
, CTFinal as (
select CCOl.Slno, CCOl.Items as ColName, CVal.Items as ColValue from CtColumn as CCOl inner join CtValue as CVal on CCOl.Slno=CVal.Slno )
select #Query = 'update ' + #TableName + ' set ' +
stuff ( (select ',' + ColName + '=' + ColValue from CTFinal for xml path ('')) ,1,1,'') +
' where ' + #IDColName + '=' + #IdColValue
End
exec sp_executesql #Query
End
Go
I have to write update using dynamic sql becaus i know only name of column that I want to update and names of columns which I will use to join tables in my update. But I don't know the numbers of tables and names. Names of tables I will get in parameter of my procedure in this way
declare #Tables = N'Customer,Employee,Owner'
So I want to have update like this:
update t
set [Status] = 100
from
TemporaryTable t
left join Customer t1 on t1.RecordId = t.RecordId
left join Employee t2 on t2.RecordId = t.RecordId
left join Owner t3 on t3.RecordId =t.RecordId
where
t1.RecordId is null
and t2.RecordId is NULL
and t3.RecordId is null
I know that each table will have column RecordId and want to left join this tables to my TemporaryTable on this column but I don't know the names and numbers of tables. For example I will have one, two, or ten tables with different names. I know that this tables names will be save in parameter #Tables in that way:
#Tables = N'Customer,Employee,Owner'
There is possilble to write this update in dynamic way?
This is an answer, which helps ... to write update using dynamic sql ... and only shows how to generate a dynamic statement. It's based on string splitting. From SQL Server 2016+ you may use STRING_SPLIT() (because here the order of the substrings is not important). For previous versions you need to find a string splitting function.
T-SQL:
DECLARE #Tables nvarchar(max) = N'Customer,Employee,Owner'
DECLARE #join nvarchar(max) = N''
DECLARE #where nvarchar(max) = N''
DECLARE #stm nvarchar(max) = N''
SELECT
#join = #join + CONCAT(
N' LEFT JOIN ',
QUOTENAME(s.[value]),
N' t',
ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
N' ON t',
ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
N'.RecordId = t.RecordId'
),
#where = #where + CONCAT(
N' AND t',
ROW_NUMBER() OVER (ORDER BY (SELECT 1)),
N'.RecordId is NULL'
)
FROM STRING_SPLIT(#Tables, N',') s
SET #stm = CONCAT(
N'UPDATE t SET [Status] = 100 ',
N'FROM TemporaryTable t',
#join,
N' WHERE ',
STUFF(#where, 1, 5, N'')
)
PRINT #stm
EXEC sp_executesql #stm
Notes:
One note, that I think is important - consider passing tables names using table value type for parameter, not as comma-separated text.
It seems like this will suit your needs, though I don't fully understand what you're trying to do. Here we're constructing the final SQL in two pieces (#s and #where) and then concatenating into the final SQL at the end.
declare #Tables varchar(100) = N'Customer,Employee,Owner'
declare #tablenames table (tablename nvarchar(100))
insert #tablenames (tablename)
select value
from string_split(#Tables, ',');
declare #where varchar(max) = ''
declare #s varchar(max) = '
update t
set [Status] = 100
from TemporaryTable t'
select #s += '
left join ' + tablename + ' on ' + tablename + '.RecordId = t.RecordId'
, #where += case when #where = '' then '' else ' and ' end + tablename + '.RecordId is null
'
from #tablenames
print #s + char(13) + ' where ' + #where
exec( #s + char(13) + ' where ' + #where)
I'm trying to replicate a crystal report with a dynamic parameter. If I type a string into the parameter screen and click the arrow button, it adds the parameter to a list:
The resulting query looks like this, but the list can grow with additional OR #param clauses:
SELECT * FROM table_name WHERE #param LIKE 'cfe%' OR #param LIKE 'abr%'
How can I create a list like that in SSRS to contain several parameters? The query below is one possible example:
SELECT * FROM table_name WHERE
#param LIKE 'cfe%'
OR #param LIKE 'abr%'
OR #param LIKE 'fez%'
OR #param LIKE 'zez%'
I tried using multiple values in the parameter, but as soon as I do I can't type in the parameter box:
I tried this but it did not work. I also tried to use CONTAINS but there are no indexes for a view.
You can use a single parameter string. The user separates entries in the list with a comma. The query got a lot worse though but I have you an example of variable number of prefixes with comma separated.
I hope this helps. Its not as simple or as pretty.
In the dataset, use a similar query or proc that uses the parameter like so and it should work:
Declare #prefixes varchar(1000)
set #prefixes='abc,defg,efgh,hij,jkl,mno'
declare #sql nvarchar(max) = ''
declare #currentint int
set #currentint = 1
declare #maxint int
set #maxint = len(#prefixes) - len(replace(#prefixes, ',', '')) + 1
declare #currentcommaposition int
set #sql = 'IF OBJECT_ID(''tempdb..#tempTest'') IS NOT NULL DROP TABLE #tempTest
create table #tempTest
(
ID INT,
name varchar(100)
)
insert into #tempTest
(id,name)
select 1,''abcd''
union
select 2, ''defghijk''
union
select 3,''efghoot''
union
select 4,''hijack''
union
select 5,''jklmo''
union
select 6,''mnopoly''
union
select 7,''pqrstuv''
union
select 8,''tubool''
IF OBJECT_ID(''tempdb..#testresults'') IS NOT NULL DROP TABLE #testresults
create table #testresults
(
id int, name varchar(100)
)
declare #prefixes varchar(100) = ''' + #prefixes + ',''' + char(10) + ' declare #currentint int declare #maxint int = ' + convert(varchar(10),#maxint) + char(10)
while ( #currentint <= #maxint )
begin
set #sql = #sql + 'set #currentint = ' + convert(varchar(10),#currentint) + ' declare #suffix' + convert(varchar(2), #currentint) + ' VARCHAR(100)' + char(10)
+ 'set #suffix' + convert(varchar(2), #currentint) + '= substring(#prefixes,0,charindex('','',#prefixes))' + char(10)
+
'set #prefixes=Right(#prefixes,len(#prefixes)-charindex('','',#prefixes))' + char(10) +
'insert into #testresults (id, name)
select id, name from #temptest t where t.name like #suffix' + convert(varchar(2), #currentint) + ' + ''%''' + char(10)
+ 'if (#currentint = #maxint) begin select * from #testresults end ' + char(10)
set #currentint = #currentint + 1
end
exec sp_executesql #sql
The second option for you will be having parameters for each suffix a user can answer and allow them to be blank or null as the default. This will limit the number of prefixes the user can enter, but I do think you should be able to guesstimate the max number a user would enter. Or the user can run the report multiple times and when they do they export to excel to mash the reports together if they want to.
This is a bit easier to understand for the developer but more work for the user.
So in your stored procedure you will then use a statement like below:
select *
from dbo.Test t
WHERE
( ISNULL(#Prefix1,'') <> '' AND t.TestName LIKE #Prefix1 + '%')
OR
( ISNULL(#Prefix2,'') <> '' AND t.TestName LIKE #Prefix2 + '%')
OR
( ISNULL(#Prefix3,'') <> '' AND t.TestName LIKE #Prefix3 + '%')
OR
( ISNULL(#Prefix4,'') <> '' AND t.TestName LIKE #Prefix4 + '%')
OR
( ISNULL(#Prefix5,'') <> '' AND t.TestName LIKE #Prefix5 + '%')
OR
( ISNULL(#Prefix6,'') <> '' AND t.TestName LIKE #Prefix6 + '%')
OR
( ISNULL(#Prefix7,'') <> '' AND t.TestName LIKE #Prefix7 + '%')
OR
( ISNULL(#Prefix8,'') <> '' AND t.TestName LIKE #Prefix8 + '%')
OR
( ISNULL(#Prefix9,'') <> '' AND t.TestName LIKE #Prefix9 + '%')
OR
( ISNULL(#Prefix10,'') <> '' AND t.TestName LIKE #Prefix10 + '%')
This solution is for an unbounded Gridview paging and having problem with the syntax of this query:
> #currTable varchar(20),
#startRowIndex int,
#maximumRows int,
#totalRows int OUTPUT
AS
DECLARE #first_id int, #startRow int
IF #startRowIndex = 1
SET #startRowIndex = 1
ELSE
SET #startRowIndex = ((#startRowIndex - 1) * #maximumRows)+1
SET ROWCOUNT #startRowIndex
DECLARE #sql varchar(250);
SET #sql = 'SELECT ID, StringID_from_Master, GUID, short_Text, lang_String, date_Changed, prev_LangString, needsTranslation, displayRecord, brief_Descrip FROM ' + #currTable + ' ';
EXECUTE(#sql);
PRINT #first_id
SET ROWCOUNT #maximumRows
SELECT #sql = 'SELECT ' + CAST(#first_id as varchar(20)) + ' = ID FROM ' + QUOTENAME(#currTable) + ' ORDER BY ID ' ;
EXEC (#sql);
SET ROWCOUNT 0
-- Get the total rows
SET #sql = 'SELECT ' + + CAST(#totalRowsas varchar(20)) + ' = COUNT(ID) FROM ' + #currTable + ' ';
EXECUTE(#sql);
RETURN
<
The errors is:
Conversion failed when converting the varchar value ''SELECT ' to data type int.
Tried also
nvarchar and varchar. = + CAST(#first_id as varchar(10)) +
If you're trying to implement paging, this is wrong in so many ways. First, you're using SET ROWCOUNT to limit to #startRowIndex, but then you're selecting ALL n rows (with no ORDER BY), then getting the first ID, then counting the total rows by selecting from the table? Might I suggest a better approach?
CREATE PROCEDURE dbo.PageSmarter
#Table NVARCHAR(128), -- table names should not be varchar(20)
#FirstRow INT,
#PageSize INT,
#TotalRows INT OUTPUT
AS
BEGIN
SET NOCOUNT ON; -- always, in every stored procedure
DECLARE
#first_id INT,
#startRow INT,
#sql NVARCHAR(MAX);
SET #sql = N'WITH x AS
(
SELECT
ID,
rn = ROW_NUMBER() OVER (ORDER BY ID)
FROM
' + #Table + '
)
SELECT rn, ID
INTO #x FROM x
WHERE rn BETWEEN ' + CONVERT(VARCHAR(12), #FirstRow)
+ 'AND (' + CONVERT(VARCHAR(12), #FirstRow)
+ ' + ' + CONVERT(VARCHAR(12), #PageSize) + ' - 1);
SELECT first_id = MIN(ID) FROM #x;
SELECT
ID, StringID_from_Master, GUID, short_Text, lang_String, date_Changed,
prev_LangString, needsTranslation, displayRecord, brief_Descrip
FROM ' + #Table + ' AS src
WHERE EXISTS
(
SELECT 1 FROM #x
WHERE ID = src.ID
);';
EXEC sp_executeSQL #sql;
SELECT #totalRows = SUM(row_count)
FROM sys.dm_db_partition_stats
WHERE [object_id] = OBJECT_ID(#Table);
END
GO
DECLARE #tr INT;
EXEC dbo.PageSmarter 'dbo.tablename', 10, 2, #tr OUTPUT;
SELECT #tr;
I haven't tested all edge cases with this specific implementation. I will confess, there are much better ways to do this, but they usually aren't complicated with the additional requirement of dynamic table names. This suggests that there is something inherently wrong with your design if you can run the exact same queries against any number of tables and get similar results.
In any case, you can review some of the (quite lengthy) discussion about various approaches to paging over at SQL Server Central:
http://www.sqlservercentral.com/articles/T-SQL/66030/
There are 62 comments following up on the article:
http://www.sqlservercentral.com/Forums/Topic672980-329-1.aspx
I am guessing your #first_id field is an int. If so, then you need to CAST/Convert your #first_id value to a string/varchar.
CAST(#first_id as varchar(10))
or
Convert(varchar(10), #first_id)
MSDN documentation on CAST/Convert for SQL server
EDIT: After looking at your query again, I notice that you are setting your #first_id = ID, This is incorrect syntax, the correct syntax would be below.
SELECT #sql = 'SELECT ID AS ' + CAST(#first_id as varchar(10)) + ' FROM ' +
QUOTENAME(#currTable) + ' ORDER BY ID ' ;
EXEC (#sql);
It appears you're trying to create an alias for your column ID. The string you're building won't result in a valid SQL statement if it contains a number. It would come out to something like this:
SELECT 123 = ID FROM dbo.MyTable ORDER BY ID
Try this:
SELECT ID AS '123' FROM dbo.MyTable ORDER BY ID
To achieve that:
SELECT #sql = 'SELECT ID AS ''' + CAST(#first_id as varchar(10)) +
''' FROM ' + QUOTENAME(#currTable) +
' ORDER BY ID ' ;
I would do it this way
create table #e (a int)
SET #sql = 'insert #e SELECT COUNT(ID) FROM ' + #currTable + ' ';
exec(#sql)
select #totalRows = a from #e
drop table #e