Search SSMS Agent Jobs for a Table Name [duplicate] - sql-server

This question already has answers here:
How to find all SQL Agent Jobs that call a given stored-proc
(3 answers)
Closed 1 year ago.
Is there a way to search the SQL Server agent jobs, at the same time with a SQL query, for a specific table name?
Something like this:
Select top 100 *
from job_1
where definition like '%table_1%'
but instead of 1 job, it's all of the jobs?

SQL Agent jobs are stored in the msdb database.
If I understand your intention, this should give you a start:
select j.name JobName, s.step_name StepName
from msdb.dbo.sysjobsteps s
join msdb.dbo.sysjobs j on j.job_id=s.job_id
where s.command like '%findme%'

Related

Query not running in SQL Server [duplicate]

This question already has answers here:
How do I 'subtract' sql tables?
(14 answers)
Closed 4 years ago.
I have this select statement that I am running in SQL Server. But it's throwing an error:
select count(*)
from
(select zip from A
minus
select zip from B)
Error:
Incorrect syntax near select
What is the issue here? I have also tried aliasing the subquery but same error happens.
There is nothing called minus in SQL Server, you need to use except.
Note, except in SQL Server is equivalent to minus of Oracle
Following query will work.
select count(*) ct
from
(
select zip from A
except
select zip from B
)t
Another issue with your code is that you need to give a alias name to the inner table you are creating.

Get all the Table names in particular database SQL Server [duplicate]

This question already has answers here:
How do I get list of all tables in a database using TSQL?
(20 answers)
Closed 6 years ago.
I am using SQL Server 2014 and want to get all the tables name in a particular database STUDENT through a SQL query.
How is it possible?
Thanks
You want to query sys.objects and look for everything with the type description 'USER_TABLE'. You could use a query like this;
SELECT
*
FROM STUDENT.sys.objects
WHERE type_desc = 'USER_TABLE'
The FROM clause has the usual format: DatabaseName.SchemaName.TableName.
Or as marc_s mentions, you can use sys.tables instead;
SELECT
*
FROM STUDENT.sys.tables

Syntax Error with my SQL Create Table As [duplicate]

This question already has answers here:
How to create a table from select query result in SQL Server 2008 [duplicate]
(6 answers)
Closed 7 years ago.
I am trying to create a new table in Microsoft SQL Server Management Studio based on two existing tables.
When I execute the query below, I get an error saying that there is an:
Incorrect syntax near the keyword 'SELECT'.
SQL code:
CREATE TABLE NEW_TABLE AS
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
I looked at various other problems, but could not find a solution to mine. Do you have any idea what could be wrong with the syntax?
Alternatively, you can use SELECT * INTO new_table statement just like this.
SELECT OLD_TABLE.A
, OLD_TABLE.B
, OTHER_OLD_TABLE.C INTO NEW_TABLE
FROM OLD_TABLE
INNER JOIN OTHER_OLD_TABLE
ON OLD_TABLE.A = OTHER_OLD_TABLE.D;
this statement will also create a new table as you required.

SQL server 2008 not supporting LIMIT [duplicate]

This question already has answers here:
MySQL LIMIT clause equivalent for SQL SERVER
(5 answers)
Closed 9 years ago.
I am trying to use LIMIT into SQL server 2008 but its not working, what can I do, I am new to SQL I have used mySQL before.
Here is the SQL query I am trying:
SELECT * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC LIMIT 1
Try SELECT TOP 1 * ...
Also see http://www.w3schools.com/sql/sql_top.asp for more details.
In MSSQL it's apparently something like SELECT TOP x * FROM ...
SELECT TOP 1 * FROM konto WHERE id_e_klientit = 9 ORDER BY koha_relizimit DESC

Execute query once in SQL Server despite being called mutiple times

I have a situation where a query might be called multiple times from multiple users, but I only want it to run once (per week) against the database. The environment is SQL Server Express so scheduling via SQL Server Agent is not an option. It needs to be 2005 compatible. I'd like to make it as lightweight as possible too, so I'm asking for suggestions. Ideally a database wide declared variable - but I don't think that SQL Server supports such a beast? Thanks
Try something like this:
IF NOT EXISTS ( -- Check if you have the current week content
SELECT *
FROM WeeklyTable
WHERE
DATEPART(YEAR, DateCr) = DATEPART(YEAR, GETDATE())
AND
DATEPART(WEEK, DateCr) = DATEPART(WEEK, GETDATE())
)
BEGIN
-- delete old content
DELETE WeeklyTable
-- insert new content
INSERT INTO WeeklyTable (MyID, MyField1, ... , MyFieldN, DateCr)
SELECT
MyID, MyField1, MyField2, GETDATE()
FROM MainTable
END
You can create indexes you need for the WeeklyTable.
One option would be SQL Scheduler as a add-on to SQL Server Express.
The other option would be to create a small command-line utility that does the querying and schedule that using the Windows Scheduler on the machine where SQL Server Express is installed.
With either of the two setups, you could select the values / numbers you need into a result table once a week, and any requests during the week would be satisfied from that one result table. SQL Server doesn't have "server-wide" variables - but you can always define a table for that purpose...

Resources