Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a database with a bunch of tables.
I want to build a query, that lists all the tables in the database, and then do various counts in those tables.
For example count "How many rows that has 'D' as country" etc.
I have figured out to do a SELECT name FROM db.sys.tables to get all the tables, just can't wrap my head around the situation that I want to do counts on the tables also.
Its the same count I want to do in all tables.
You can use the undocumented system procedure sp_msforeachtable like this
DECLARE #results TABLE(tbl VARCHAR(100),countresult INT);
INSERT INTO #results
EXEC sp_msforeachtable #command1 = 'SELECT ''?'',Count(*) FROM ? /* Add your WHERE clause here*/',#replacechar = '?'
SELECT * FROM #results
You can read more about it here and here
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have data streaming from an external program to TABLE1 in my SQL Server database. Let's say TABLE1 consists of columns data1 and data2. How do I update to another table (TABLE2) when a value changes in TABLE1?
For the sake of an example, let's say a value in the data1 column changes.
Note: I wrote a trigger that works, but this is only on the basis of a manual update. I can't figure out how to automate this, meaning the trigger would compare new and old values from the table on its own and then perform the update if a value is different.
Something like below would work,
CREATE TRIGGER after_update_table1
AFTER UPDATE ON table1
FOR EACH ROW
BEGIN
IF OLD.val1<> new.val1 THEN
/**
-- do operations on table2
**/
END IF;
END;
-- if val1 allows null we need to check by adding more conditions to the if clause like,
IF((OLD.val1 IS NULL AND NEW.val1 IS NOT NULL) OR (OLD.val1 IS NOT NULL AND NEW.val1 IS NULL) OR (OLD.val1<>NEW.val1)) THEN
/**
-- operations on table2
**/
END IF;
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to Substring the string which is delimited
ABC/123/DEF
I want in
ABC in 1st column
123 in 2nd Column
DEF in 3rd Column
If your database in sql server you can try this:
declare #t table (name varchar(50))
insert into #t values ('ABC/123/DEF')
select PARSENAME(replace(name,'/','.'),3),PARSENAME(replace(name,'/','.'),2) , PARSENAME(replace(name,'/','.'),1) from #t
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am unable to create a simple conditional trigger to insert data to a new table. Below is the trigger and its output:
CREATE TRIGGER [dbo].[test_trigger]
ON [dbo].[Export_Data]
FOR INSERT
AS
BEGIN
INSERT INTO test (Terminal_ID, EmpIdentification, SwipeDateTime)
SELECT
DeviceId, UserId, LogDate
FROM
Inserted
END
OUTPUT is as follows:
My requirement is that I need logs of only Terminal_ID = 32 & 33 only.
Thank you
Just add a where clause to the select statement from the Inserted table. If you're inserting Export_Data.DeviceId into test.Terminal_ID, then your filter should be where Inserted.DeviceId in (32,33):
CREATE TRIGGER [dbo].[test_trigger] on [dbo].[Export_Data]
for insert
as
begin
insert into test(Terminal_ID,EmpIdentification,SwipeDateTime)
select DeviceId,UserId,LogDate from Inserted
WHERE DeviceId in (32, 33)
end
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
In sql server, when I create a column like:
select *, '1' as test
what is the type of this new column i.e 1 ?
Just one more question, can I reference this column on other table?
For SQL Server, you can answer such questions easily using sp_describe_first_result_set:
exec sp_describe_first_result_set N'select *, ''1'' as test from sys.objects'
Produces a result set, the last row of which indicates that the test column in that result set is of type varchar(1). (Other results in this case, pulling the other columns from the sys.objects table make it clear that this procedure is capable of describing columns as non-var char(2) or nvarchar(128), so it's not just a display issue)
This column has char(1) type and yes u can use this column in joins or other manipulation etc as well.
because its value is a string within ' ' If you want it to be an integer. you can do the following:
select *, Cast(1 as INT) as test FROM SomeTable
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Table data is
1-"A11111,B22222,C33333,D44444,B22222"
2-"A11111,C33333,D44444,B22222,D44444"
3-"A11111,E55555,C33333,D44444,B22222"
If will use
select *
from table
where data = 'E55555'
can get number 3 data
Thanks
And yet another variant
SELECT * FROM [table] WHERE ','+[data]+',' LIKE '%,E55555,%'
The problem though, is that what you are asking for won't be able to use normal indexes, which means an inefficient table scan every time you query. You may want to consider breaking the comma delimited values into their own columns so you can query on them efficiently. As it is you have a 'super' column that crams multiple pieces of data into a single column.
Try this:
SELECT * FROM [table] WHERE [data] LIKE '%E55555%'
This query will help you to handle comma separated data search:
SELECT * FROM [table] WHERE
[data] ='E55555' OR -- IF record contains only this value
[data] LIKE 'E55555,%' OR -- If record starts with the value followed by comma and others
[data] LIKE '%,E55555' OR -- If record ends with the value preceeded by comma and others
[data] LIKE '%,E55555,%' -- If record contains the value in middle
Refer SQL Fiddle here