SQL Query: How to query a function/procedure body-script - sql-server-2005

is there any Query syntax (predefined )in SQL Server can show a function/procedure body-script?
sth like:
Select * from ShowScript('MyFunctionOrProcedureName')

I think you want
exec sp_helptext 'MyFunctionOrProcedureName'

Related

Linked Server: MO_GLOBAL.SET_POLICY_CONTEXT('S', 83);

I have a linked server (SQL Server 2008 to Oracle) and would like to know how to execute this statement before I query the data:
MO_GLOBAL.SET_POLICY_CONTEXT('S', 83);
I can query the data using this:
Select * from OPENQUERY (linkedservername, 'Select * from tablename')
But I'm not sure how to incorporate the set_policy_context statement. Appreciate any feedback.
Here's the correct statement:
EXECUTE('BEGIN apps.MO_GLOBAL.SET_POLICY_CONTEXT(''S'', 83); END;') at tablename;

Use Dynamic Query in a Procedure and call that procedure in a select statement in SQL Server

I need to execute a dynamic query in SQL Server and show the result in a select query.
Since, we cannot use dynamic queries in functions, we have written a stored procedure to return the dynamic query value.
But how can i call my stored procedure in a select query.
You normally would use the key word EXEC or EXECUTE to call a stored procedure.
yet there is a way of selecting data from a stored procedure using OPENQUERY.
SELECT * FROM
OPENQUERY(SERVERNAME, 'EXECUTE Proc_Name #parameters')

How to extract part of SQL query from a table

Is it possible to to extract SQL queries that are saved in a table?
For example
select * from saved_queries
name | statement
queryname | 'select * from mytable where myfield = 'somevalue'
I would like to be able to do something like
select * from ( extractsomehow( 'select Statement from saved_queries where name = 'queryname') ).
Unfortunately I cannot use Java so I am restricted to SQL and XML there.
I am using Oracle 11g
If you can write a stored procedure, you could use execute immediate, something like this:
select statement into v_statement from saved_queries where ... ;
execute immediate v_statement;
Before you use dynamic SQL, think carefully about whether or not you actually need it.

How do I execute a stored procedure on linked Firebird server in SQL Server 2008

I have a linked Firebird database on my SQL Server 2008 via ODBC.
I can execute a query like this and I get the wanted results:
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'SELECT * FROM TABLE_NAME')
Now i wonder how can I execute stored procedure with one parameter input.
I have tried:
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'STORED_PROCEDURE_NAME 00001')
and
EXEC LINKED_SERVER_NAME.STORED_PROCEDURE_NAME '00001'
with no success...
Any tip would be appreciated !
I dont know in MSSQL but you can try
SELECT * FROM OPENQUERY(LINKED_SERVER_NAME, 'SELECT * FROM STORED_PROCEDURE_NAME(00001)')

check for zero rows in select query

My C program has an embedded sql query. This program runs on windows and queries the oracle database.
The query is similar to EXEC SQL SELECT ...
I need to add here a check to know if the query returns zero rows.
Basically I want to set a local valiable to know my query has returned no rows and
handle this condition accordingly.
How can I add it. I know that EXISTS statement can be used. But I am not getting
how do I use it in embedded sql.
Thanks for any help.
Use the sqlca struct
EXEC SQL include "sqlca.h"
#define NO_ROWS_FOUND (sqlca.sqlcode==1403)
EXEC SQL BEGIN DECLARE SECTION;
int val=0;
short ind=0;
EXEC SQL END DECLARE SECTION;
EXEC SQL
select value
int :val :ind
from mytable where rownum=1;
if(NO_ROWS_FOUND)
printf("No rows found\n");
Use SELECT COUNT(*) FROM ... and compare result to 0.