search Unicode Text in Stored Procedure Using SQL - sql

I wrote the following stored procedure in SQL Server Mgmt Studio 2008:
CREATE PROCEDURE [dbo].[SearchName]
#Name nvarchar(5)=''
AS
BEGIN
SELECT * FROM tblNames WHERE FirstName=N'#Name'
END
I need to search by Unicode text. For example, if I call this procedure with 'فارسی', the stored procedure should run this: FirstName=N'فارسی'.

Try this one -
CREATE PROCEDURE [dbo].[SearchName]
#Name NVARCHAR(5) = N''
AS BEGIN
SELECT *
FROM dbo.tblNames
WHERE FirstName = #Name
END

Related

Storing one or several sql statements as a variable in a stored procedure in SQL Server

Let's say I have a stored procedure. Is it possible to store multiple sql statements in a variable inside a stored procedure and later execute it when the procedure is invoked?
Example:
CREATE PROCEDURE dbo.StoredProcedure
(
#parameter1 int = QUERIES_HERE
)
AS
/*something like execute #parameter1*/
RETURN
Yes, like this:
DECLARE #SQLQuery varchar(500)
SET #SQLQuery = 'SELECT * FROM Employees WHERE EmployeeID = 123'
EXECUTE(#SQLQuery)

How to change datatype of parameter of SQL procedure without changing its body

Let's imagine that we have a stored procedure
CREATE PROCEDURE [dbo].[do_some_magic]
#text nvarchar(100)
AS
BEGIN
-- procedure body
END
And now we need to change parameter #text datatype from nvarchar(100) to nvarchar(2000). Procedure body should not be changed.
How to do this without putting whole procedure body in an alter SQL statement?
ALTER PROCEDURE [dbo].[do_some_magic]
#text nvarchar(2000)
-- can we omit following part?
AS
BEGIN
-- procedure body
END
You can try a dynamic SQL solution that preserves the current body and only changes the pieces that you need. Something like this: (depending on how the stored procedure body is you may need to be more clever with the replace to avoid changing something else unintentionally)
declare #alter nvarchar(max)
SELECT #alter = ROUTINE_DEFINITION
FROM [INFORMATION_SCHEMA].[ROUTINES] as r
WHERE r.ROUTINE_NAME = 'do_some_magic'
AND r.ROUTINE_SCHEMA = 'dbo'
SET #alter = replace(replace(#alter,
'CREATE PROCEDURE','ALTER PROCEDURE'),
'#text nvarchar(100)','#text nvarchar(2000)')
exec (#alter)

Stored Procedure Parameter with FORMSOF and Thesaurus (SQL Server 2008 R2 SP2)

I have created a stored procedure
CREATE PROCEDURE [dbo].[GetTableColumns]
#search_phrase varchar(100)
AS
BEGIN
SET NOCOUNT ON;
SELECT *
from SearchTableColumn stc
where contains(*, 'FORMSOF(Thesaurus, #search_phrase)')
order by stc.weight desc
END
The stored procedure was created without any problems in SQL Server 2008 R2 SP2. If I execute the stored procedure in SQL Server Management Studio like this:
exec GetTableColumns 'instrument'
I got no records returned.
However, if I query the database with the following SQL,
SELECT *
from SearchTableColumn stc
where contains(*, 'FORMSOF(Thesaurus, instrument)')
order by stc.weight desc
I got records returned.
So I am wondering if something I am missing in the stored procedure which prevents the records from being returned?
Thanks in advance.
You are searching for a literal 'FORMSOF(Thesaurus,#search_phrase)' in your table.
Try setting the search value using the passed in param:
CREATE PROCEDURE [dbo].[GetTableColumns]
#search_phrase varchar(100)
AS
BEGIN
SET NOCOUNT ON;
Declare #search varchar(120) = 'FORMSOF(Thesaurus,' + #search_phrase + ')'
SELECT *
from SearchTableColumn stc
where contains(*, #search)
order by stc.weight desc
END

Issue in passing parameter on stored procedure

I have created a procedure without any problem. I have tested before creating the procedure but when I try to execute my procedure I have a problem only in the first parameter I have
SQL SERVER 2008 SHOW ME Error syntax in 'Mundus'
ProcAvancementTotalbyEtab 'Erasmus Mundus','C:\Users\AA\Desktop\Table1.xlsx'
and After I try to make like this:
ProcAvancementTotalbyEtab N'Erasmus Mundus','C:\Users\AA\Desktop\Table1.xlsx'
This is my stored procedure:
alter proc ProcAvancementbyEtab
(
#nameEtabb nvarchar(50),
#FilePath nvarchar(60)
)
as
begin
EXEC
(
'
SET NOCOUNT OFF;
insert into Avancement(nameEtab)
SELECT distinct T.[EFP]
From OPENROWSET(''MICROSOFT.ACE.OLEDB.12.0'',
''Excel 12.0;Database='+#FilePath+''',
''SELECT *
FROM [Sheet1$]'') T
where T.[EFP] is not null and T.[Avancement] is not null
and
not exists (select * from Avancement where
Avancement.nameEtab=T.[EFP])
and T.[EFP]='+#nameEtabb+'
')
end
When I comment this line
and T.[EFP]='+#nameEtabb+'
it works fine so my probleme is here
and T.[EFP]='+#nameEtabb+'
and when I test without stored procedure it work also OMG.
My guess is that name is stored as a string. You need an extra pair of quotes:
and T.[EFP] = '''+#nameEtabb+'''

How to query from a stored procedure in SQL Server?

Let say I have a simple Stored Procedure:
ALTER PROCEDURE [dbo].[myProc]
AS
BEGIN
SELECT * FROM myTable
END
How can I do a WHERE statement in Microsoft SQL Server Management Studio to the stored procedure? Something like that:
SELECT * FROM myProc WHERE x = 'a'; -- But that doesn't work...
It sounds like you're trying to make a "dynamic" stored procedure.
Something you might want to do is:
1) Insert the contents of your stored procedure into a temporary table
2) Use dynamic sql to apply a where condition to that temporary table.
Something like:
declare #as_condition varchar(500); --Your condition
create table #a
(
id bigint
)
insert into #a
execute sproc
declare #ls_sql varchar(max);
set #ls_sql = "select * from #a where " + #as_condition;
execute (#ls_sql);
SQL Server allows you to use INSERT INTO to grab a stored procedure's output. For example, to grab all processes with SPID < 10, use:
create table #sp_who (
spid smallint,
ecid smallint,
status nchar(30),
loginame nchar(128),
hostname nchar(128),
blk char(5),
dbname nchar(128),
cmd nchar(16),
request int)
insert into #sp_who execute sp_who
select * from #sp_who where spid < 10
You can't add a WHERE clause to a stored procedure like this.
You should put the clause in the sproc, like this:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X
END
GO
The syntax for calling a stored procedure is through the use of EXECUTE not SELECT(e.g.):
EXECUTE dbo.myProc 'a'
I think you can't do that.
The command to execute a stored procedure is EXECUTE.
See some more examples of the EXECUTE usage.
I think its better to use a view or a table valued function rather than the suggested approach. Both allow you to pass parameters to the function
If you want the WHERE clause to be something you can "turn off" you can do this, passing in a predetermined value (e.g. -1) if the WHERE limitation is to be bypassed:
ALTER PROCEDURE [dbo].[myProc]
#X VARCHAR(10)
AS
BEGIN
SELECT * FROM myTable WHERE x=#X or #X = -1
END
GO
You must declare a variable in the store procedure which will be necessary to pass to run the stored procedure. Here is an example. Keep this in mind: Before AS you can simply declare any variable by using the # character, but after the AS you must write Declare to declare any variable, e.g., Declare #name nvarchar (50).
ALTER PROCEDURE [dbo].[myProc]
#name varchar (50)
AS
BEGIN
SELECT * FROM myTable
where name= #name
END