ASP VBscript get sql script of a stored procedure - sql

With microsoft sql server manager i know how to get this problem solved, but on the hosting where i am trying to update a old ASP Vbscript website i dont have access to this manager. and my question is:
Is possible from ASP VBscript environement to access a stored procedure sql script ? and to see what sql queries were used for this stored procedure ?
I know how to show their names but also i am interested in their content to know what tables do not touch with my modifications.

Here's a nice little tutorial example
First, show all the stored procedures in the database:
SELECT * FROM sysobjects WHERE type = 'P' AND category = 0 ORDER BY name
Next, retrieve a stored procedure's contents :
SELECT text
FROM syscomments
WHERE id = (SELECT id FROM sysobjects WHERE name = '{0}')
ORDER BY colid

You could run a query like this:
SELECT definition
FROM sys.sql_modules
WHERE object_id = OBJECT_ID('YourProcedureName')

Related

Using a query that is stored in a table

My database has two tables: t_computers and t_queries.
This query shows me which computers are laptops
select *
from t_computers
where type = 'Laptop'
In the table t_queries I have stored dynamic SQL queries.
SELECT QuerySQL
from t_query
where QueryName = 'Clients that have not been started in 30 days'
The first result is the SQL query that would give me this information.
Now for the complicated part, I want to only select computers that have the type 'Laptop' and are returned if I run the query that is stored in the table.
So something like this
select *
from t_computers
where type = 'Laptop' and
(computer is returned for (SELECT QuerySQL
from query
where QueryName = 'Clients that have not been started in 30 days'))
Is this even possible? I am using SQL Server 2008 R2
I have used a very simplified example.
Some background information on why I want to use the query saved in the table: With our Client Management System (similar to SCCM) Administrators can easily create "views" of Clients. For example Filtering out all Computers that have an IP starting with 10.*. As soon as they save the view, a SQL query is created and saved in the table t_queries. This one query that I want to compare against changes quite often.
Yes it is possible, but as said by the commenters, I strongly unadvise you to execute arbitrary code coming from your users, no matter how much you trust them. You would have very little possibilities to enforce security rules and may open yourself to devastating security breaches.
The way it is properly done in other systems is to use a specific query language (custom or not) that you interpret and "translate" to SQL if needed. That allows you to limit the possible operations to what is strictly necessary.
After that disclaimer, here is an answer to your question (untested, I don't have SQL Server on this laptop so I may have messed up a bit with the quotes) :
exec('select *
from t_computers
where type = ''Laptop'' and
(computer is returned for ('+SELECT TOP(1) QuerySQL
from query
where QueryName = 'Clients that have not been started in 30 days'+'))');

Need to identify a table in all objects in database

I need to identify a table that is mentioned anywhere in database (in stored proc, views, and, etc.). I tried to find a query online, but couldn't find it. Any help would be great!
I use the free SQL Search plugin for MS Management Studio for things like that: http://www.red-gate.com/products/sql-development/sql-search/
I often use this snippet when I'm looking for dependencies. In this case, you would replace the text with what you're searching (assuming you're on MS SQL Server):
USE [DBNAME]
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%enter_search_here%'
GROUP BY OBJECT_NAME(id)
You can also look for specific object types by adding a check for object property:
WHERE OBJECTPROPERTY(id, 'IsTable') = 1
Here is a LIST of useful object properties!

not able to copy data from one table to another in sql server

i am using new installed sql server 2008
i am trying to copy data from one table to another..i wrote query like this;
insert into Clr select * from Color_tbl
but this is showing error like this:
Invalid object name 'Clr'.
what i have to do? i have change any setting?
clr is not an existing table..i hop if i execute this query that will create clr table automatically...
I used to copy data from one table to another table like this: but i dont know what happend after installing new Sql server??
any help is very appriciable
Without knowing anything else about your database and schemas, you could try using fully qualified names, like so:
INSERT INTO [DBContainingClr].[SchemaContainingClr].Clr
SELECT * FROM [DBContainingColor_tbl].[SchemaContainingColor_tbl].Color_tbl
UPDATE: OP has clarified that he is in fact trying to CREATE the table Clr, and it doesn't exist at the moment. In which case the syntax should be:
SELECT * INTO Clr FROM Color_Tbl

Specify name of table types in SQL Server [duplicate]

This question already has an answer here:
SQL Server create User-Defined Table Types with schema not working properly
(1 answer)
Closed 9 years ago.
Create a new database in MS SQL Server 2008 R2 and then create a new table type with the following command
CREATE TYPE typeMyType AS TABLE (ID INT)
Now execute the following query
SELECT OBJECT_NAME (object_id) AS ObjectName, *
FROM sys.indexes
WHERE index_id <= 1
ORDER BY ObjectName
This will show you that an object of type HEAP was created which is fine as the data for typeMyType has to be stored somewhere.
But the object is called TT_typeMyType_01142BA1 in my case.
Question:
Why isn't it called typeMyType and how can I overwrite this obviously server generated name?
The table type is listed in several places that can be looked at via the following system tables (sys.sysobjects, sys.indexes, sys.table_types and sys.types).
I can understand that table_types is a subset of types. Therefore, those two places are where to look for my table type AssociativeArray in my AdventureWorks2012 database.
The question I have is why is it looking like a table in sysobjects and sysindexes?
We have not defined any use for the variable yet. However, it looks like a table. It must be how the engine defines meta data for future use.
One take away, does the information in sysobjects and sysindexes get updated a run-time when we declare a variable of type AssociativeArray?
Also, what happens when two SPIDS create the same variable at the same time with different data being inserted?
That is a in-depth engine question that maybe a someone from Microsoft CAT team might know off the top of their head.
I guess I could do some research to find out.
Enclosed is a link stating that table variables use tempdb. That is what I know is a fact. Again, I wonder if the sys.objects and sys.indexes get updated or just place holders.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html

SSMS: How to view a stored procedure/view/function without scripting?

i want to look at the definition of a stored procedure, view, or user-defined function.
In SQL Server Management Studio 2005 the only way i've found to do this is:
Right-click
Script stored procedure as
ALTER To
New Query Editor Window
goto 1
i don't want to script it, i want to look at it.
My task today in SSMS is to quickly go through the stored procedures to find one i'm interested in. i've loaded up Enterprise Manager in Windows XP mode (MMC snap-in doesn't run nativly in 64-bit), and my job is much easier:
Push enter
goto 1
i'm trying to find the way to look at a stored procedure - i'm not interested in scripting it.
I did some quick google searches and found this.
Copy and Paste from website:
-- Get Stored Procedure Content
-- Name = Stored Procedure Name.
-- Colid = Multiple lines, their sequence.
SELECT text
FROM syscomments
WHERE id = (SELECT id FROM sysobjects WHERE name = '{0}')
ORDER BY colid
Depending on where the information is, have you tried to filter them "to find one I'm interested in"?
SELECT text
FROM syscomments c
INNER JOIN sysobjects o
ON o.id = c.id
WHERE o.type = 'P'
and o.Name = '{0}'
FOR XML PATH('')
exec sp_helptext N'<stored proc name>'
Example:
exec sp_helptext N'mydatabase.dbo.myStoredProc'
This will shows all the lines of the procedure without having to maneuver the GUI to the sp.
Using syscomments has potential problem: big procedures are splitted in several rows and sometimes important identifiers are divided into 2 parts. Like:
row1: .....veryimportantide
row2: ntifier.....
so, if you are looking for veryimportantidentifier - you will never find it. (for example - looking for all references of it)
When I look for something and it is very important - I generate scripts of all objects and navigate there using something like Notepad++ or Visual Studio
I found a solution for preview the stored function in SQL Server Management Studio 2012. It is non-programmer solution :)
Find chosen function in Object Explorer.
Click on it right mouse button.
Choose Modify.
Function preview is displayed. You can copy it to NotePad ++ and analyse.
I hope this will be helpful.