I have a sql query to get the count of a table
select count(*) from table_name_ID;
the above query is working as expected but the ID in table name has to be fetched from another parameter table and execute it in shell script and output has to be mailed to the user
tried multiple ways and failed, could someone help me out here?
Try like this:
DECLARE #Query VARCHAR(5000)
SET #Query ='BCP.EXE "SELECT * FROM '+DB_NAME()+'..Table_Name" queryout "D:\TEST\Test.txt" -T -c'
EXEC master..xp_cmdshell #Query
Related
I'm working on a query that builds a list of table names from the sys.database master table. I then use those name in a concat statement to pull a specific piece if information out of each database. I'm running the whole thing through a while loop to hit all tables, with a counter.
i.e.
set #sql = 'select top 10 * from ' + (select dbname from #table where tabid = #i + '.dbo.**tablename** where NAME = '**String Value**'
When I just print the results of #sql, it gives me the correct syntax, and I am able to run the command with no problem.
When I set #sql to exec, I get an error
"database select top 10 * from dbname does not exist."
It's like the execute is ignoring everything after the select.
I'm suspecting you're calling EXEC incorrectly.
When calling 'exec' with a T-SQL query, be sure to enclose the target string in parenthesis, eg
DECLARE #FOO VARCHAR(100)
Set #FOO = 'SELECT TOP 5 * from SOMETABLE'
EXEC (#FOO) -- not EXEC #FOO
I am trying to write some dynamic SQL queries that select results into a temp table with a query string. It looks like follows:
DECLARE #SQL Varchar(4000)
SET #SQL = 'SELECT * INTO #tmp_tab FROM dbo.sometable'
EXEC(#SQL)
It doesn't give any error to run the code, but when I want to select from #tmp_tab, it says the table doesn't exist.
So I am wondering if there is any special syntax for it, or dynamic SQL doesn't support such operation?
Many thanks.
Maybe it has something to do with access. If you create a global temp table, it will work.
DECLARE #SQL Varchar(4000)
SET #SQL = 'SELECT * INTO ##tmp_tab FROM dbo.batch'
EXEC(#SQL)
SELECT * FROM ##tmp_tab
If I open a connection to a database, for example, with SSMS, and I run a query like this:
SELECT * FROM MySchema.MyTable
the query is executed without error.
If I run it as dynamic sql like this
declare var #qry nvarchar(200);
set #qry = N'SELECT * FROM MySchema.MyTable'
exec master.sys.sp_executesql #qry
I get an error stating that the table doesn't exist.
If I put the database name prefix before MySchema.MyTable name, i.e. MyDb.MySchema.MyTable the query runs correctly.
How can I avoid the error without specifying the database name in the dyanmic SQL?
try this!
change this
exec master.sys.sp_executesql #qry
to
exec sp_executesql #qry
or
exec #qry
Both answers by Recursive and koushik veldanda work fine, but this gives a deeper insight to the problem:
The problem is that executing a query with
exec master.sys.sp_execute_sql #qry
changes the context to the master database, so MySchema.MyTable is not accesible, because it doesn't belong to master, but to MyDb.
If the query is executed with
exec sys.sp_execute_sql #qry
the context is maintained in the current database, so the table is accesible, and the query runs without problem.
Use
exec #sql
is enough if there are any output parameters then go with
exec sp_executesql
I have to debug a large stored procedure in SQL Server 2008 (also in 2005).
because I can't go in that procedure StepByStep, I need to debug it using some output files.
actually i use something like
DECLARE #SQL VARCHAR(8000)
SELECT #SQL = 'BCP "SELECT * FROM MY_TABLE" QUERYOUT "D:\TDB\test.txt" -C -T -w'
EXEC MASTER..XP_CMDSHELL #SQL
but this approach has multiple limitations like impossibility to use # tables, and a complex way to use filters (where X='+cast(#MYLocalVar as varchar)+')...
Is there a other way to output a select to a file, like MySql does
SELECT * into outfile '../../htdocs/VIP/Temp/temp.txt' from tmp_Menu2;
You can put the data into a table quite readily:
SELECT *
into anotherdatabase..outtable
from . . .
This creates a new table with whatever columns you want.
To output something into a file requires an extra step, using bulk export.
I want to dump a tale into a text file I am using following command -
bcp tablename out myTable.dat -T -C
But got error it says incorrect symbol near bcp.
I am not sure why I am getting this.
From #Code Monkey's Link : http://www.simple-talk.com/sql/database-administration/creating-csv-files-using-bcp-and-stored-procedures/
try this
declare #sql varchar(8000)
select #sql = 'bcp [dbname].[dbo].[tablename] out myTable.dat -c -t, -T -S'+ ##servernameexec
master..xp_cmdshell #sql
where dbname is the name of your database, and replace dbo with the name of the schema if not dbo