Selecting data from more than 2000 databases? - sql

How i can search / select a field in more than 2000 databases in sql server.
i have a main database consisting a table called 'Kewword" where i store key world under 'kewwordtitle' field in keyword table, when new user register a new database are created for the user and user use a keyword,
now the situtation is, how i can find that how much user use a key work, here keywordtitle is primary key,...
thanks/

Your question is a little bit fuzzy, but if this is a one shot and if all your databases are on the same instance, you could do something like:
declare #t table(col int)
insert #t
exec sp_MSforeachdb 'use ?; select 1 from keyword where keywordtitle = ''<yourkeyword>'''
select count(*) from #t

Related

SQL Server - Select INTO statement stored in sys.tables

I know how to find the CREATE statement for a table in SQL Server but is there any place that stores the actual SQL code if I use SELECT INTO ... to create a table and if so how do I access it?
I see two ways of creating tables with SELECT INTO.
First: You know the Schema, then you can declare a #Table Variable and perform the Select INSERT
Second: You can create a temp table:
SELECT * INTO #TempTable FROM Customer
There are some limitations on the second choice:
- You need to drop the temp table afterwards.
- If there is a VARCHAR Column and the maximum number of characters of that given SELECT is 123 characters (example), and then you try to insert into the TEMP table afterwards with a greater number of characters, it will throw an error.
My recommendation is always declare a table in order to use, it makes it clear what is the intentions and increases readability.

Unable to access one of the table in SQL Server database

I have a production database which has 200 tables. Since last week I am unable to access one of the tables. When I just select top 100 rows it keeps on running.
How can I find out why the table is not accessible? How can I find if there is any lock on the table? All the other tables are running fine
From what I understood, you are not able to get any results when you query it.
There may be a lot of reasons for that.
1) It could be locked.
To do a dirty read, try querying with NOLOCK hint.
SELECT Column1 FROM TableName WITH (NOLOCK)
To check if there are locks on the table, use the script below:
declare #a table (
spid int,
[dbid] int,
objid int,
indid int,
[type] varchar(10),
resource varchar(100),
mode varchar(2),
[status] varchar(20)
);
insert into #a
exec sp_lock
select object_name(objid) tablename, * from #a where object_name(objid) = 'TableName'
2) Queries might be slow when statistics are outdated. Try updating them.
UPDATE STATISTICS dbo.TableName;
3) The TOP operator itself. The top operator basically takes the entire set of data and sorts it and gives you the first 100. You can add query hints to get some data before it is sorted.
SELECT TOP 10 Column1 FROM TableName (OPTION FAST(1))
--Have avoided doing a `SELECT * FROM....`
SELECT 1 FROM TableName (OPTION FAST(1))
--Without `TOP`
Check Permissions on the table. Right click on the table and select properties. Click on the permissions tab and make sure you have access to that table.

SQL Server - Generate script without primary key

I'm trying to make a generated script of my data (I mean, all the INSERT INTO commands).
Because access permissions, I can't do a SET IDENTITY_INSERT TABLE OFF and ON (I'm using the user application in Staging)
So, there is a way to make this script in SQL Server Manager and avoid the field with the primary key?
I set to false all properties (primary, unique, etc), but the script is still sending this field (For e.g., RecID 1, 2, 3, etc).
I'm using SQL Server 2012.
My configuration for the script:
Results I get:
SET IDENTITY_INSERT -TABLE- ON
INSERT INTO TABLE (ID,Field1) VALUES (1,'value')
Any solution (except for removing it with Notepad++) is appreciated.
A bit of a work around, but sometimes useful quick and dirty way of doing these things:
SELECT 'INSERT INTO TABLE (Field1) VALUES (''' + Field1 + ''')' FROM TABLE
The result set will be a row for each insert statement for every row in the table TABLE. The INSERT statement is generated from concatenating the INSERT statement text with the values in Field1.
There is another way to do this which is a bit more automatic and a lot less faffy when you have a lot of columns of different data types; given a table T:
(
ID int identity,
C1 nvarchar(255),
C2 datetime
...
)
...select everything except the identity column into a new table:
select C1, C2, ... into InterimTable from T
Then:
Run the Generate Scripts wizard on InterimTable.
Use whatever tool you have to search the SQL for InterimTable and replace with T
Run

C# SqlParameter - provide SQL (Microsoft SQL)

I am currently tasked with a project on a database whose schema cannot be changed. I need to insert a new row into a table that requires an ID to be unique, but the original creators of the structure did not set this value to autoincrement. To go around this, I have been using code akin to:
(SELECT TOP 1 [ID] from [Table] ORDER BY [ID] DESC) + 1
when giving the value of the ID field, basically having an inner query of sorts. Problem is that a few lines down, I need that ID I just inputted. If I could set a SQLParameter to output for this column, I could get the value it was set to, problem is I'm using SQL, and not a hard value like I do with other SQLParameters. Can't I use SQL in place of just a value?
This is a potential high volume exchange, so I'd rather not do 2 different queries (one to get id, then one to insert).
You say you cannot change the schema, but can you add an additional table to the project that does an autoincrement column? Then you could use that table to (safely) create your new IDs and return them to your code.
This is similar to how Oracle does IDs, and sometimes vendor applications for sql server that also run on Oracle will use that approach just to help minimize the differences between the two databases.
Update:
Ah, I just spotted your comment to the other answer here. In that case, the only other thing I can think that might work is to put your two statements (insert a new ID, and then read back the new ID) inside a transaction with the SERIALIZABLE isolation level. And that just kinda sucks, because it leaves you open to performance and locking gotchas.
Is it possible for you to create a stored procedure in the database to do this and the return value of the stored procedure will then return the ID that you need?
I'm a bit confused about where you need to use this ID. If it inside of the same stored proc just use this method:
DECLARE #NewId int
SELECT TOP 1 #NewId = [ID] + 1 from [Table] ORDER BY [ID] DESC
SELECT #NewId
You can put more than one SQL statement in a single SqlCommand. So you could easily do something along the lines of what Abe suggested:
DECLARE #NewId int
SELECT TOP 1 #NewId = [ID] + 1 from [Table] ORDER BY [ID] DESC
INSERT INTO [Table] (ID, ...) VALUES (#NewId, ...)
SELECT #NewId
Then you just call ExecuteScalar on your SqlCommand, and it will do the INSERT and then return the ID it used.

How to create a stored procedure which pulls data from multiple tables that are not linked What do i do

I need to create a stored procedure which pulls data from multiple tables that are not linked what do i do .Basically i got a few tables that are not linked and all i need to do is pull all the data from all those tables.
PLEASE HELP MY LIFE DEPENDS ON THIS
Just do several selects?
select * from MyTable1;
select * from MyTable2;
select * from MyTable3;
You can then access each of those tables from your calling code. I'd include an example on how to do that, but you don't include any details about what language you are calling from.. details I would have included if my life depended on it :)
If you mean you have rows in several tables, and you want to pull them all back then you can do that by doing a UNION.
What do you mean by 'not linked'? Are they on the same sql server instance?
If so, just select from the full schema name.
Eg.
Select * from database1.dbo.table
Select * from database2.dbo.table
If you want a single result set you can use the JOIN keyword whether or not the tables have foreign key relationships defined; just specify which columns you would like to join the tables on.
Alternatively, you can use multiple result sets and just have multiple SELECT statements.
Your questions is not clear, but if you are trying to pull just certain things from certain tables and then return them to the user, can do something like this:
CREATE PROCEDURE spMyTestProc
AS
DECLARE #F1 int
DECLARE #F2 int
DECLARE #F3 int
DECLARE #f4 char(10)
SELECT #f1 = FIELD1 from MYTABLE1
SELECT #F2 = FIELD2 FROM MYTABLE2
SELECT #F3 = FIELD3, #F4=FIELD4 FROM MYTABLE3
/* NOW return the result set to the user, it'll come back just like a regular select */
SELECT #F1 AS F1, #F2 AS F2, #F3 AS F3, #F4=F4
The presence or absence of primary or foreign keys doesn't prevent you from joining the tables. (It won't be particularly fast, but you can do it.) Neither does their existing in separate database instances.
You say that your databases aren't linked. From this, I have to infer that the database you're writing the stored procedure in likely has no access to the 2nd database; if that's the case, you won't be able to access it anyway. You need to add a link to the 2nd database using Enterprise Manager.
Once you've done that, you can reference the tables in the 2nd database using its schema name as below:
SELECT *
FROM foo.employees
where foo is the schema name.
Good luck.
SELECT Field1, Field2, Field3 FROM Table1
UNION ALL
SELECT F1, F2, F3 FROM Table2
UNION ALL
SELECT Fld1, Fld2, Fld3 FROM Table3;
This will pull all data from all three tables into single output.
Check that other table fields match type of the first table and you are good to go.